diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs index 0fc5e36..2cb0921 100644 --- a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs @@ -1,13 +1,28 @@ -using Microsoft.Extensions.DependencyInjection; +using AutoMapper; +using Microsoft.Extensions.DependencyInjection; namespace DigitalData.Core.Infrastructure.AutoMapper; public static class DependencyInjection { - public static IServiceCollection AddEntityAutoMapper(this IServiceCollection services, Action entityAutoMapperOptionsAction) + public static EntityConfigurationOptions UseAutoMapper(this EntityConfigurationOptions options, params Type[] typeOfDtos) { - EntityAutoMapperOptions options = new(services); - entityAutoMapperOptionsAction.Invoke(options); - return services; + IMapper? customRootMapper = null; + + if (typeOfDtos.Length != 0) + { + MapperConfigurationExpression AutoMapperConfiguration = new(); + + foreach (var typeOfDto in typeOfDtos) + { + AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto); + AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity)); + } + + customRootMapper = new MapperConfiguration(AutoMapperConfiguration).CreateMapper(); + } + + options.AddCustomMapper(provider => new EntityAutoMapper(customRootMapper ?? provider.GetRequiredService())); + return options; } } diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj index 6a1f700..cb10076 100644 --- a/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj +++ b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj @@ -8,6 +8,7 @@ + diff --git a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs deleted file mode 100644 index 5aabf06..0000000 --- a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs +++ /dev/null @@ -1,33 +0,0 @@ -using AutoMapper; -using DigitalData.Core.Abstractions.Infrastructure; -using Microsoft.Extensions.DependencyInjection; - -namespace DigitalData.Core.Infrastructure.AutoMapper; - -public class EntityAutoMapperOptions -{ - public readonly MapperConfigurationExpression AutoMapperConfiguration = new (); - - private readonly IServiceCollection _services; - - private readonly Lazy _lazyCustomRootMapper; - - internal EntityAutoMapperOptions(IServiceCollection services) - { - _services = services; - _lazyCustomRootMapper = new(() => new MapperConfiguration(AutoMapperConfiguration).CreateMapper()); - } - - public void CreateEntityMap(params Type[] typeOfDtos) - { - foreach (var typeOfDto in typeOfDtos) - { - AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto); - AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity)); - } - - _services.AddSingleton, EntityAutoMapper>(provider => new EntityAutoMapper(_lazyCustomRootMapper.Value)); - } - - public void CreateEntityMap() => _services.AddSingleton, EntityAutoMapper>(); -} diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs index 9d656bd..ee55815 100644 --- a/DigitalData.Core.Infrastructure/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -6,12 +6,14 @@ namespace DigitalData.Core.Infrastructure; public static class DependencyInjection { - public static IServiceCollection AddDbRepository(this IServiceCollection services, Func> queryFactory) + public static EntityConfigurationOptions AddDbRepository(this IServiceCollection services, Func> queryFactory) where TDbContext : DbContext where TEntity : class { - return services + services .AddScoped, DbRepository>() .AddSingleton(queryFactory); + + return new EntityConfigurationOptions(services); } } \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs new file mode 100644 index 0000000..852907d --- /dev/null +++ b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs @@ -0,0 +1,20 @@ +using DigitalData.Core.Abstractions.Infrastructure; +using Microsoft.Extensions.DependencyInjection; + +namespace DigitalData.Core.Infrastructure; + +public class EntityConfigurationOptions +{ + private readonly IServiceCollection _services; + + public EntityConfigurationOptions(IServiceCollection services) + { + _services = services; + } + + public EntityConfigurationOptions AddCustomMapper(Func> factory) + { + _services.AddSingleton(factory); + return this; + } +}