diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs new file mode 100644 index 0000000..0fc5e36 --- /dev/null +++ b/DigitalData.Core.Infrastructure.AutoMapper/DependencyInjection.cs @@ -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 entityAutoMapperOptionsAction) + { + EntityAutoMapperOptions options = new(services); + entityAutoMapperOptionsAction.Invoke(options); + return services; + } +} diff --git a/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj new file mode 100644 index 0000000..6a1f700 --- /dev/null +++ b/DigitalData.Core.Infrastructure.AutoMapper/DigitalData.Core.Infrastructure.AutoMapper.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapper.cs b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapper.cs new file mode 100644 index 0000000..eb6970f --- /dev/null +++ b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapper.cs @@ -0,0 +1,20 @@ +using AutoMapper; +using DigitalData.Core.Abstractions.Infrastructure; + +namespace DigitalData.Core.Infrastructure.AutoMapper; + +public class EntityAutoMapper : IEntityMapper +{ + private readonly IMapper _rootMapper; + + public EntityAutoMapper(IMapper rootMapper) + { + _rootMapper = rootMapper; + } + + public TDto Map(TEntity entity) => _rootMapper.Map(entity); + + public TEntity Map(TDto dto) => _rootMapper.Map(dto); + + public TEntity Map(TDto dto, TEntity entity) => _rootMapper.Map(dto, entity); +} diff --git a/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs new file mode 100644 index 0000000..5aabf06 --- /dev/null +++ b/DigitalData.Core.Infrastructure.AutoMapper/EntityAutoMapperOptions.cs @@ -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 _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.sln b/DigitalData.Core.sln index 5437a0f..3935ce5 100644 --- a/DigitalData.Core.sln +++ b/DigitalData.Core.sln @@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDF84A84-1E01-484E-B073-383F7139C891}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection 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} {C57B2480-F632-4691-9C4C-8CC01237203C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {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} {72CBAFBA-55CC-49C9-A484-F8F4550054CB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {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 GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}