diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs index 2cb0921..96bab09 100644 --- a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs @@ -1,5 +1,4 @@ -using AutoMapper; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; namespace DigitalData.Core.Infrastructure.AutoMapper; @@ -7,22 +6,21 @@ public static class DependencyInjection { public static EntityConfigurationOptions UseAutoMapper(this EntityConfigurationOptions options, params Type[] typeOfDtos) { - IMapper? customRootMapper = null; - - if (typeOfDtos.Length != 0) + options.AddCustomMapper>(services => { - MapperConfigurationExpression AutoMapperConfiguration = new(); - - foreach (var typeOfDto in typeOfDtos) + if (typeOfDtos.Length != 0) { - AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto); - AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity)); + services.AddAutoMapper(cnf => + { + foreach (var typeOfDto in typeOfDtos) + { + cnf.CreateMap(typeof(TEntity), typeOfDto); + cnf.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/EntityConfigurationOptions.cs b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs index 852907d..c3ab1bb 100644 --- a/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs +++ b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs @@ -12,9 +12,16 @@ public class EntityConfigurationOptions _services = services; } - public EntityConfigurationOptions AddCustomMapper(Func> factory) + public EntityConfigurationOptions AddCustomMapper(Action configure, Func? factory = null) + where TEntityMapper : class, IEntityMapper { - _services.AddSingleton(factory); + configure(_services); + + if (factory is null) + _services.AddSingleton, TEntityMapper>(); + else + _services.AddSingleton(factory); + return this; } }