如何在使用程序集扫描时访问.NET AutoMapper配置访问?

huangapple go评论65阅读模式
英文:

How to access .NET AutoMapper configuration access when using assembly scanning?

问题

在使用程序集扫描来注册配置文件时,我能否获取 AutoMapper 配置实例的访问权限?
https://docs.automapper.org/en/latest/Dependency-injection.html

我之前使用了 ForAllMaps() 来为所有映射添加一个通用的 IgnoreUnmappedProperties()

public static class AutoMapperExtensions
{
    private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
    {
        foreach (string propName in map.GetUnmappedPropertyNames())
        {
            if (map.SourceType.GetProperty(propName) != null)
            {
                expr.ForSourceMember(propName, opt => opt.Ignore());
            }
            if (map.DestinationType.GetProperty(propName) != null)
            {
                expr.ForMember(propName, opt => opt.Ignore());
            }
        }
    }

    public static void IgnoreUnmapped(this IProfileExpression profile)
    {
        profile.ForAllMaps(IgnoreUnmappedProperties);
    }

    public static void IgnoreUnmapped(this IProfileExpression profile, Func<TypeMap, bool> filter)
    {
        profile.ForAllMaps((map, expr) =>
        {
            if (filter(map))
            {
                IgnoreUnmappedProperties(map, expr);
            }
        });
    }

    public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
    {
        profile.IgnoreUnmapped((TypeMap map) => map.SourceType == src && map.DestinationType == dest);
    }

    public static void IgnoreUnmapped<TSrc, TDest>(this IProfileExpression profile)
    {
        profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));
    }
}

我看不到如何在新的依赖注入模式中添加这个功能。是否有人可以指向我需要完成此操作的文档,或告诉我是否应该使用其他模式来实现相同的功能?:o)

英文:

Can I get access to the AutoMapper configuration instance when using assembly scanning to register the profiles.
https://docs.automapper.org/en/latest/Dependency-injection.html

I had previously used ForAllMaps() to add a generic IgnoreUnmappedProperties() for all mappings.

public static class AutoMapperExtensions
{
    private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
    {
        foreach (string propName in map.GetUnmappedPropertyNames())
        {
            if (map.SourceType.GetProperty(propName) != null)
            {
                expr.ForSourceMember(propName, opt =&gt; opt.Ignore());
            }
            if (map.DestinationType.GetProperty(propName) != null)
            {
                expr.ForMember(propName, opt =&gt; opt.Ignore());
            }
        }
    }

    public static void IgnoreUnmapped(this IProfileExpression profile)
    {
        profile.ForAllMaps(IgnoreUnmappedProperties);
    }

    public static void IgnoreUnmapped(this IProfileExpression profile, Func&lt;TypeMap, bool&gt; filter)
    {
        profile.ForAllMaps((map, expr) =&gt;
        {
            if (filter(map))
            {
                IgnoreUnmappedProperties(map, expr);
            }
        });
    }

    public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
    {
        profile.IgnoreUnmapped((TypeMap map) =&gt; map.SourceType == src &amp;&amp; map.DestinationType == dest);
    }

    public static void IgnoreUnmapped&lt;TSrc, TDest&gt;(this IProfileExpression profile)
    {
        profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));
    }

}

I am not seeing how I might add this in the new DI pattern. Can someone point me to the docs I need to accomplish this, or tell me I'm dumb and should use this other pattern to achieve the same thing? : o)

答案1

得分: 2

.AddAutoMapper() 的重载版本,接受一个 Action<IMapperConfigurationExpression> 或者 Action<IServiceProvider, IMapperConfigurationExpression> 作为第一个参数。

例如:

    private static void ConfigureAutoMapper(this IServiceCollection services)
    {
        services.AddAutoMapper(
           config => config.ForAllMaps(IgnoreUnmappedProperties),
           new List<Assembly> { typeof(AutoMapperExtensions).Assembly });
    }
英文:

There are overrides of .AddAutoMapper() that accept an Action&lt;IMapperConfigurationExpression&gt; or even Action&lt;IServiceProvider, IMapperConfigurationExpression&gt; as the first argument.

For instance:

    private static void ConfigureAutoMapper(this IServiceCollection services)
    {
        services.AddAutoMapper(
           config =&gt; config.ForAllMaps(IgnoreUnmappedProperties),
           new List&lt;Assembly&gt; { typeof(AutoMapperExtensions).Assembly });
    }

huangapple
  • 本文由 发表于 2023年7月7日 02:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定