34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
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<IMapper> _lazyCustomRootMapper;
|
|
|
|
internal EntityAutoMapperOptions(IServiceCollection services)
|
|
{
|
|
_services = services;
|
|
_lazyCustomRootMapper = new(() => new MapperConfiguration(AutoMapperConfiguration).CreateMapper());
|
|
}
|
|
|
|
public void CreateEntityMap<TEntity>(params Type[] typeOfDtos)
|
|
{
|
|
foreach (var typeOfDto in typeOfDtos)
|
|
{
|
|
AutoMapperConfiguration.CreateMap(typeof(TEntity), typeOfDto);
|
|
AutoMapperConfiguration.CreateMap(typeOfDto, typeof(TEntity));
|
|
}
|
|
|
|
_services.AddSingleton<IEntityMapper<TEntity>, EntityAutoMapper<TEntity>>(provider => new EntityAutoMapper<TEntity>(_lazyCustomRootMapper.Value));
|
|
}
|
|
|
|
public void CreateEntityMap<TEntity>() => _services.AddSingleton<IEntityMapper<TEntity>, EntityAutoMapper<TEntity>>();
|
|
}
|