feat(EntityAutoMapper): Erstellt mit der Konfiguration der Dependency Injection.

This commit is contained in:
Developer 02 2025-04-22 15:15:52 +02:00
parent 3c1bbc1151
commit 65e834784a
5 changed files with 90 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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>

View File

@ -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);
}

View File

@ -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>>();
}

View File

@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Infrastructure.AutoMapper", "DigitalData.Core.Infrastructure.AutoMapper\DigitalData.Core.Infrastructure.AutoMapper.csproj", "{CE00E1F7-2771-4D9C-88FB-E564894E539E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{41795B74-A757-4E93-B907-83BFF04EEE5C}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -92,12 +96,16 @@ Global
{C9266749-9504-4EA9-938F-F083357B60B7}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9266749-9504-4EA9-938F-F083357B60B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.Build.0 = Release|Any CPU {C9266749-9504-4EA9-938F-F083357B60B7}.Release|Any CPU.Build.0 = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE00E1F7-2771-4D9C-88FB-E564894E539E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{A765EBEA-3D1E-4F36-869B-6D72F87FF3F6} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {A765EBEA-3D1E-4F36-869B-6D72F87FF3F6} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
{DB404CD9-CBB8-4771-AB1B-FD4FDE2C28CC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {DB404CD9-CBB8-4771-AB1B-FD4FDE2C28CC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{C57B2480-F632-4691-9C4C-8CC01237203C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {C57B2480-F632-4691-9C4C-8CC01237203C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{B54DEF90-C30C-44EA-9875-76F1B330CBB7} = {EDF84A84-1E01-484E-B073-383F7139C891} {B54DEF90-C30C-44EA-9875-76F1B330CBB7} = {EDF84A84-1E01-484E-B073-383F7139C891}
@ -111,6 +119,8 @@ Global
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6} = {EDF84A84-1E01-484E-B073-383F7139C891} {9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6} = {EDF84A84-1E01-484E-B073-383F7139C891}
{72CBAFBA-55CC-49C9-A484-F8F4550054CB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {72CBAFBA-55CC-49C9-A484-F8F4550054CB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{C9266749-9504-4EA9-938F-F083357B60B7} = {72CBAFBA-55CC-49C9-A484-F8F4550054CB} {C9266749-9504-4EA9-938F-F083357B60B7} = {72CBAFBA-55CC-49C9-A484-F8F4550054CB}
{CE00E1F7-2771-4D9C-88FB-E564894E539E} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
{41795B74-A757-4E93-B907-83BFF04EEE5C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94} SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}