feat: add mapping
This commit is contained in:
parent
cfbd847d9a
commit
9e9d4e06fd
@ -13,6 +13,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="FluentValidation" Version="11.9.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
|
||||
<PackageReference Include="MediatR" Version="12.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
28
Application/Common/Mappings/AssemblyMappingProfile.cs
Normal file
28
Application/Common/Mappings/AssemblyMappingProfile.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using AutoMapper;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application.Common.Mappings;
|
||||
|
||||
public class AssemblyMappingProfile : Profile
|
||||
{
|
||||
public AssemblyMappingProfile(Assembly assembly) =>
|
||||
ApplyMappingsFromAssembly(assembly);
|
||||
|
||||
private void ApplyMappingsFromAssembly(Assembly assembly)
|
||||
{
|
||||
var types = assembly.GetExportedTypes()
|
||||
.Where(type => type.GetInterfaces()
|
||||
.Any(i => i.IsGenericType &&
|
||||
i.GetGenericTypeDefinition() == typeof(IMapWith<>)))
|
||||
.ToList();
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var instance = Activator.CreateInstance(type);
|
||||
var methodInfo = type.GetMethod("Mapping");
|
||||
methodInfo?.Invoke(instance, new[] { this });
|
||||
}
|
||||
}
|
||||
}
|
9
Application/Common/Mappings/IMapWith.cs
Normal file
9
Application/Common/Mappings/IMapWith.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application.Common.Mappings;
|
||||
|
||||
public interface IMapWith<T>
|
||||
{
|
||||
void Mapping(Profile profile) =>
|
||||
profile.CreateMap(typeof(T), GetType());
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
using MediatR;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
using Mirea.Api.DataAccess.Application.Common.Behaviors;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user