diff --git a/Application/Application.csproj b/Application/Application.csproj index c03a6c1..2003517 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -13,6 +13,10 @@ + + + + diff --git a/Application/Common/Mappings/AssemblyMappingProfile.cs b/Application/Common/Mappings/AssemblyMappingProfile.cs new file mode 100644 index 0000000..68a9e8e --- /dev/null +++ b/Application/Common/Mappings/AssemblyMappingProfile.cs @@ -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 }); + } + } +} \ No newline at end of file diff --git a/Application/Common/Mappings/IMapWith.cs b/Application/Common/Mappings/IMapWith.cs new file mode 100644 index 0000000..390e4e0 --- /dev/null +++ b/Application/Common/Mappings/IMapWith.cs @@ -0,0 +1,9 @@ +using AutoMapper; + +namespace Mirea.Api.DataAccess.Application.Common.Mappings; + +public interface IMapWith +{ + void Mapping(Profile profile) => + profile.CreateMap(typeof(T), GetType()); +} \ No newline at end of file diff --git a/Application/DependencyInjection.cs b/Application/DependencyInjection.cs index 2686e15..61c3eab 100644 --- a/Application/DependencyInjection.cs +++ b/Application/DependencyInjection.cs @@ -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;