refactor(EntityConfigurationOptions): aktualisiert, um IServiceCollection mit Callback zu konfigurieren
This commit is contained in:
parent
8d98159ba8
commit
91594e80bf
@ -1,5 +1,4 @@
|
|||||||
using AutoMapper;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Infrastructure.AutoMapper;
|
namespace DigitalData.Core.Infrastructure.AutoMapper;
|
||||||
|
|
||||||
@ -7,22 +6,21 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> options, params Type[] typeOfDtos)
|
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> options, params Type[] typeOfDtos)
|
||||||
{
|
{
|
||||||
IMapper? customRootMapper = null;
|
options.AddCustomMapper<EntityAutoMapper<TEntity>>(services =>
|
||||||
|
|
||||||
if (typeOfDtos.Length != 0)
|
|
||||||
{
|
{
|
||||||
MapperConfigurationExpression AutoMapperConfiguration = new();
|
if (typeOfDtos.Length != 0)
|
||||||
|
|
||||||
foreach (var typeOfDto in typeOfDtos)
|
|
||||||
{
|
{
|
||||||
AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto);
|
services.AddAutoMapper(cnf =>
|
||||||
AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity));
|
{
|
||||||
|
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<TEntity>(customRootMapper ?? provider.GetRequiredService<IMapper>()));
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,9 +12,16 @@ public class EntityConfigurationOptions<TEntity>
|
|||||||
_services = services;
|
_services = services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityConfigurationOptions<TEntity> AddCustomMapper(Func<IServiceProvider, IEntityMapper<TEntity>> factory)
|
public EntityConfigurationOptions<TEntity> AddCustomMapper<TEntityMapper>(Action<IServiceCollection> configure, Func<IServiceProvider, TEntityMapper>? factory = null)
|
||||||
|
where TEntityMapper : class, IEntityMapper<TEntity>
|
||||||
{
|
{
|
||||||
_services.AddSingleton(factory);
|
configure(_services);
|
||||||
|
|
||||||
|
if (factory is null)
|
||||||
|
_services.AddSingleton<IEntityMapper<TEntity>, TEntityMapper>();
|
||||||
|
else
|
||||||
|
_services.AddSingleton(factory);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user