feat(EntityConfigurationOptions): Erstellt, um Entitäten wie Mapper konfigurieren zu können
This commit is contained in:
@@ -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<EntityAutoMapperOptions> entityAutoMapperOptionsAction)
|
||||
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> 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<TEntity>(customRootMapper ?? provider.GetRequiredService<IMapper>()));
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user