From fdb33a0539e34f9c83d8775385cb8737aa5a8903 Mon Sep 17 00:00:00 2001 From: AliReZa Sabouri Date: Sat, 29 Jun 2024 16:35:55 +0200 Subject: [PATCH] refactor: test and improve DI extension method --- src/Gridify/DependencyInjectionExtensions.cs | 27 +++++----- .../Gridify.Tests/DependencyInjectionTests.cs | 51 +++++++++++++++++++ test/Gridify.Tests/Gridify.Tests.csproj | 2 + 3 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 test/Gridify.Tests/DependencyInjectionTests.cs diff --git a/src/Gridify/DependencyInjectionExtensions.cs b/src/Gridify/DependencyInjectionExtensions.cs index 20d597de..44d3046c 100644 --- a/src/Gridify/DependencyInjectionExtensions.cs +++ b/src/Gridify/DependencyInjectionExtensions.cs @@ -10,31 +10,28 @@ namespace Gridify; public static class DependencyInjectionExtensions { /// - /// Automatically scans an assembly for classes that inherit from GridifyMapper and registers them in the Dependency Injection container. + /// Automatically scans an assembly for classes that inherit from GridifyMapper<T> and registers them in the Dependency Injection container. /// /// The IServiceCollection to which the mappers should be added. /// The assembly to scan for GridifyMapper implementations. /// The service lifetime for the registered mappers (default is Singleton). 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; } } diff --git a/test/Gridify.Tests/DependencyInjectionTests.cs b/test/Gridify.Tests/DependencyInjectionTests.cs new file mode 100644 index 00000000..15e408a9 --- /dev/null +++ b/test/Gridify.Tests/DependencyInjectionTests.cs @@ -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>().Should().NotBeNull(); + serviceProvider.GetService>().Should().NotBeNull(); + serviceProvider.GetService().Should().BeNull(); + services.Count.Should().Be(2); + } + + private static Assembly GetAssembly() + { + var assemblyMock = Substitute.For(); + 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; + + public class AnotherTestMapper : GridifyMapper; + + + // Non-Gridify types for testing + public class NonGridifyMapper; +} diff --git a/test/Gridify.Tests/Gridify.Tests.csproj b/test/Gridify.Tests/Gridify.Tests.csproj index 71b9e6da..725a2a52 100644 --- a/test/Gridify.Tests/Gridify.Tests.csproj +++ b/test/Gridify.Tests/Gridify.Tests.csproj @@ -10,7 +10,9 @@ + + all