using AutoMapper; using DigitalData.Core.Abstractions.Infrastructure; using Microsoft.Extensions.DependencyInjection; namespace DigitalData.Core.Infrastructure.AutoMapper; public class EntityAutoMapperOptions { public readonly MapperConfigurationExpression AutoMapperConfiguration = new (); private readonly IServiceCollection _services; private readonly Lazy _lazyCustomRootMapper; internal EntityAutoMapperOptions(IServiceCollection services) { _services = services; _lazyCustomRootMapper = new(() => new MapperConfiguration(AutoMapperConfiguration).CreateMapper()); } public void CreateEntityMap(params Type[] typeOfDtos) { foreach (var typeOfDto in typeOfDtos) { AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto); AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity)); } _services.AddSingleton, EntityAutoMapper>(provider => new EntityAutoMapper(_lazyCustomRootMapper.Value)); } public void CreateEntityMap() => _services.AddSingleton, EntityAutoMapper>(); }