samedi 7 mai 2022

Mapping profile instance doesn't creates

I'm trying to add automapping models with reflection, I created an interface IMapFrom<> and implemented it in all dtos. Then I created class

public class MappingProfile : Profile
{
    public MappingProfile(Assembly assembly)
        => ApplyMappingsFromAssembly(assembly);

    private void ApplyMappingsFromAssembly(Assembly assembly)
    {
        var types = assembly
            .GetExportedTypes()
            .Where(t => t
                .GetInterfaces()
                .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
            .ToList();

        foreach (var type in types)
        {
            var instance = Activator.CreateInstance(type);

            const string mappingMethodName = "Mapping";

            var methodInfo = type.GetMethod(mappingMethodName)
                             ?? type.GetInterface("IMapFrom`1")?.GetMethod(mappingMethodName);

            methodInfo?.Invoke(instance, new object[] { this });
        }
    }
}

And add it in service collection

public static IServiceCollection AddAutoMapperProfile(IServiceCollection services, Assembly assembly)
         => services
             .AddAutoMapper(
                 (_, config) => config
                     .AddProfile(new MappingProfile(Assembly.GetCallingAssembly())),
                 Array.Empty<Assembly>());

Why the class instance is not created? Because of this I can't convert the model into a dto





Aucun commentaire:

Enregistrer un commentaire