feat(EntityAutoMapper): Erstellt mit der Konfiguration der Dependency Injection.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure.AutoMapper;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddEntityAutoMapper(this IServiceCollection services, Action<EntityAutoMapperOptions> entityAutoMapperOptionsAction)
|
||||
{
|
||||
EntityAutoMapperOptions options = new(services);
|
||||
entityAutoMapperOptionsAction.Invoke(options);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstractions.Infrastructure;
|
||||
|
||||
namespace DigitalData.Core.Infrastructure.AutoMapper;
|
||||
|
||||
public class EntityAutoMapper<TEntity> : IEntityMapper<TEntity>
|
||||
{
|
||||
private readonly IMapper _rootMapper;
|
||||
|
||||
public EntityAutoMapper(IMapper rootMapper)
|
||||
{
|
||||
_rootMapper = rootMapper;
|
||||
}
|
||||
|
||||
public TDto Map<TDto>(TEntity entity) => _rootMapper.Map<TDto>(entity);
|
||||
|
||||
public TEntity Map<TDto>(TDto dto) => _rootMapper.Map<TEntity>(dto);
|
||||
|
||||
public TEntity Map<TDto>(TDto dto, TEntity entity) => _rootMapper.Map(dto, entity);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
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>>();
|
||||
}
|
||||
Reference in New Issue
Block a user