feat(EntityConfigurationOptions): Erstellt, um Entitäten wie Mapper konfigurieren zu können

This commit is contained in:
Developer 02 2025-04-22 16:21:57 +02:00
parent 65e834784a
commit 3955dede16
5 changed files with 45 additions and 40 deletions

View File

@ -1,13 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure.AutoMapper;
public static class DependencyInjection
{
public static IServiceCollection AddEntityAutoMapper(this IServiceCollection services, Action<EntityAutoMapperOptions> entityAutoMapperOptionsAction)
public static EntityConfigurationOptions<TEntity> UseAutoMapper<TEntity>(this EntityConfigurationOptions<TEntity> options, params Type[] typeOfDtos)
{
EntityAutoMapperOptions options = new(services);
entityAutoMapperOptionsAction.Invoke(options);
return services;
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;
}
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@ -1,33 +0,0 @@
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>>();
}

View File

@ -6,12 +6,14 @@ namespace DigitalData.Core.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
public static EntityConfigurationOptions<TEntity> AddDbRepository<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>> queryFactory)
where TDbContext : DbContext
where TEntity : class
{
return services
services
.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>()
.AddSingleton(queryFactory);
return new EntityConfigurationOptions<TEntity>(services);
}
}

View File

@ -0,0 +1,20 @@
using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure;
public class EntityConfigurationOptions<TEntity>
{
private readonly IServiceCollection _services;
public EntityConfigurationOptions(IServiceCollection services)
{
_services = services;
}
public EntityConfigurationOptions<TEntity> AddCustomMapper(Func<IServiceProvider, IEntityMapper<TEntity>> factory)
{
_services.AddSingleton(factory);
return this;
}
}