29 lines
1003 B
C#

using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure.AutoMapper;
public static class DependencyInjection
{
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> options, params Type[] typeOfDtos)
{
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;
}
}