From 3955dede169e2d6b850b80b3c97ea66eb2f2bb2d Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 22 Apr 2025 16:21:57 +0200 Subject: [PATCH] =?UTF-8?q?feat(EntityConfigurationOptions):=20Erstellt,?= =?UTF-8?q?=20um=20Entit=C3=A4ten=20wie=20Mapper=20konfigurieren=20zu=20k?= =?UTF-8?q?=C3=B6nnen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DependencyInjection.cs | 25 +++++++++++--- ...Data.Core.Infrastructure.AutoMapper.csproj | 1 + .../EntityAutoMapperOptions.cs | 33 ------------------- .../DependencyInjection.cs | 6 ++-- .../EntityConfigurationOptions.cs | 20 +++++++++++ 5 files changed, 45 insertions(+), 40 deletions(-) delete mode 100644 DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs create mode 100644 DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs index 0fc5e36..2cb0921 100644 --- a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs @@ -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 entityAutoMapperOptionsAction) + public static EntityConfigurationOptions UseAutoMapper(this EntityConfigurationOptions 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(customRootMapper ?? provider.GetRequiredService())); + return options; } } diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj index 6a1f700..cb10076 100644 --- a/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj +++ b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj @@ -8,6 +8,7 @@ + diff --git a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs deleted file mode 100644 index 5aabf06..0000000 --- a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs +++ /dev/null @@ -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 _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>(); -} diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs index 9d656bd..ee55815 100644 --- a/DigitalData.Core.Infrastructure/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -6,12 +6,14 @@ namespace DigitalData.Core.Infrastructure; public static class DependencyInjection { - public static IServiceCollection AddDbRepository(this IServiceCollection services, Func> queryFactory) + public static EntityConfigurationOptions AddDbRepository(this IServiceCollection services, Func> queryFactory) where TDbContext : DbContext where TEntity : class { - return services + services .AddScoped, DbRepository>() .AddSingleton(queryFactory); + + return new EntityConfigurationOptions(services); } } \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs new file mode 100644 index 0000000..852907d --- /dev/null +++ b/DigitalData.Core.Infrastructure/EntityConfigurationOptions.cs @@ -0,0 +1,20 @@ +using DigitalData.Core.Abstractions.Infrastructure; +using Microsoft.Extensions.DependencyInjection; + +namespace DigitalData.Core.Infrastructure; + +public class EntityConfigurationOptions +{ + private readonly IServiceCollection _services; + + public EntityConfigurationOptions(IServiceCollection services) + { + _services = services; + } + + public EntityConfigurationOptions AddCustomMapper(Func> factory) + { + _services.AddSingleton(factory); + return this; + } +}