using AutoMapper; using Microsoft.Extensions.DependencyInjection; namespace DigitalData.Core.Infrastructure.AutoMapper; public static class DependencyInjection { public static EntityConfigurationOptions UseAutoMapper(this EntityConfigurationOptions 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(customRootMapper ?? provider.GetRequiredService())); return options; } }