英文:
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 => 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));
}
}
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<IMapperConfigurationExpression>
or even Action<IServiceProvider, IMapperConfigurationExpression>
as the first argument.
For instance:
private static void ConfigureAutoMapper(this IServiceCollection services)
{
services.AddAutoMapper(
config => config.ForAllMaps(IgnoreUnmappedProperties),
new List<Assembly> { typeof(AutoMapperExtensions).Assembly });
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论