Skip to content

Commit

Permalink
feat: add automatic QueryBuilder registration to DI
Browse files Browse the repository at this point in the history
refactor: remove duplicate code

fix: DI registration

test: Dependency Injection methods

refactor: remove unnecessary changes

refactor: simplify
  • Loading branch information
alirezanet committed Jun 29, 2024
1 parent ffb6366 commit 31db533
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/Gridify/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,28 @@ namespace Gridify;
public static class DependencyInjectionExtensions
{
/// <summary>
/// Automatically scans an assembly for classes that inherit from GridifyMapper<T> and registers them in the Dependency Injection container.
/// Automatically scans an assembly for classes that inherit from GridifyMapper&lt;T&gt; and registers them in the Dependency Injection container.
/// </summary>
/// <param name="services">The IServiceCollection to which the mappers should be added.</param>
/// <param name="assembly">The assembly to scan for GridifyMapper implementations.</param>
/// <param name="lifetime">The service lifetime for the registered mappers (default is Singleton).</param>
public static IServiceCollection AddGridifyMappers(this IServiceCollection services, Assembly assembly,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
var mapperType = typeof(GridifyMapper<>);

var mapperTypes = assembly.GetTypes()
var targetTypes = assembly.GetTypes()
.Where(type =>
type is { IsAbstract: false, IsGenericTypeDefinition: false, BaseType.IsGenericType: true } &&
type.BaseType.GetGenericTypeDefinition() == mapperType);
type is { IsAbstract: false, IsGenericTypeDefinition: false, BaseType.IsGenericType: true, BaseType.Namespace: nameof(Gridify) } &&
type.BaseType.GetGenericTypeDefinition() == typeof(GridifyMapper<>));

foreach (var mapper in mapperTypes)
foreach (var type in targetTypes)
{
var genericArguments = mapper.BaseType?.GetGenericArguments();
if (genericArguments is { Length: 1 })
{
var targetType = genericArguments[0];
var interfaceType = typeof(IGridifyMapper<>).MakeGenericType(targetType);
services.Add(new ServiceDescriptor(interfaceType, mapper, lifetime));
}
var genericArguments = type.BaseType?.GetGenericArguments();
if (genericArguments is not { Length: 1 }) continue;
var targetType = genericArguments[0];
var interfaceType = typeof(IGridifyMapper<>).MakeGenericType(targetType);
services.Add(new ServiceDescriptor(interfaceType, type, lifetime));
}

return services;
}
}
51 changes: 51 additions & 0 deletions test/Gridify.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Reflection;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;

namespace Gridify.Tests;

public class DependencyInjectionTests
{

[Fact]
public void AddGridifyMappers_ShouldRegisterMappers()
{
// Arrange
var services = new ServiceCollection();
var assemblyMock = GetAssembly();

// Act
services.AddGridifyMappers(assemblyMock);

// Assert
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<IGridifyMapper<TestEntity>>().Should().NotBeNull();
serviceProvider.GetService<IGridifyMapper<TestEntity2>>().Should().NotBeNull();
serviceProvider.GetService<TestMapper>().Should().BeNull();
services.Count.Should().Be(2);
}

private static Assembly GetAssembly()
{
var assemblyMock = Substitute.For<Assembly>();
assemblyMock.GetTypes().Returns([
typeof(TestMapper),
typeof(AnotherTestMapper),
typeof(NonGridifyMapper),
]);
return assemblyMock;
}
public record TestEntity(string P1, string P2);

public record TestEntity2(string P1, string P2);

public class TestMapper : GridifyMapper<TestEntity>;

public class AnotherTestMapper : GridifyMapper<TestEntity2>;


// Non-Gridify types for testing
public class NonGridifyMapper;
}
2 changes: 2 additions & 0 deletions test/Gridify.Tests/Gridify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<ItemGroup>
<PackageReference Include="AutoBogus" Version="2.13.1" />
<PackageReference Include="FluentAssertions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 31db533

Please sign in to comment.