first commit

This commit is contained in:
2026-02-26 14:04:18 +07:00
parent 57ac80a666
commit 4b7236493f
92 changed files with 4999 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
using AutoMapper;
namespace MyNewProjectName.Application.Mappings;
/// <summary>
/// Interface for auto mapping registration
/// </summary>
public interface IMapFrom<T>
{
void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
}

View File

@@ -0,0 +1,55 @@
using System.Reflection;
using AutoMapper;
namespace MyNewProjectName.Application.Mappings;
/// <summary>
/// AutoMapper profile that auto-registers mappings from IMapFrom interface
/// </summary>
public class MappingProfile : Profile
{
public MappingProfile()
{
ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly());
}
private void ApplyMappingsFromAssembly(Assembly assembly)
{
var mapFromType = typeof(IMapFrom<>);
var mappingMethodName = nameof(IMapFrom<object>.Mapping);
bool HasInterface(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == mapFromType;
var types = assembly.GetExportedTypes()
.Where(t => t.GetInterfaces().Any(HasInterface))
.ToList();
var argumentTypes = new Type[] { typeof(Profile) };
foreach (var type in types)
{
var instance = Activator.CreateInstance(type);
var methodInfo = type.GetMethod(mappingMethodName);
if (methodInfo != null)
{
methodInfo.Invoke(instance, new object[] { this });
}
else
{
var interfaces = type.GetInterfaces().Where(HasInterface).ToList();
if (interfaces.Count > 0)
{
foreach (var @interface in interfaces)
{
var interfaceMethodInfo = @interface.GetMethod(mappingMethodName, argumentTypes);
interfaceMethodInfo?.Invoke(instance, new object[] { this });
}
}
}
}
}
}