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

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

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

问题

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

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

  1. public static class AutoMapperExtensions
  2. {
  3. private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
  4. {
  5. foreach (string propName in map.GetUnmappedPropertyNames())
  6. {
  7. if (map.SourceType.GetProperty(propName) != null)
  8. {
  9. expr.ForSourceMember(propName, opt => opt.Ignore());
  10. }
  11. if (map.DestinationType.GetProperty(propName) != null)
  12. {
  13. expr.ForMember(propName, opt => opt.Ignore());
  14. }
  15. }
  16. }
  17. public static void IgnoreUnmapped(this IProfileExpression profile)
  18. {
  19. profile.ForAllMaps(IgnoreUnmappedProperties);
  20. }
  21. public static void IgnoreUnmapped(this IProfileExpression profile, Func<TypeMap, bool> filter)
  22. {
  23. profile.ForAllMaps((map, expr) =>
  24. {
  25. if (filter(map))
  26. {
  27. IgnoreUnmappedProperties(map, expr);
  28. }
  29. });
  30. }
  31. public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
  32. {
  33. profile.IgnoreUnmapped((TypeMap map) => map.SourceType == src && map.DestinationType == dest);
  34. }
  35. public static void IgnoreUnmapped<TSrc, TDest>(this IProfileExpression profile)
  36. {
  37. profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));
  38. }
  39. }

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

  1. public static class AutoMapperExtensions
  2. {
  3. private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
  4. {
  5. foreach (string propName in map.GetUnmappedPropertyNames())
  6. {
  7. if (map.SourceType.GetProperty(propName) != null)
  8. {
  9. expr.ForSourceMember(propName, opt =&gt; opt.Ignore());
  10. }
  11. if (map.DestinationType.GetProperty(propName) != null)
  12. {
  13. expr.ForMember(propName, opt =&gt; opt.Ignore());
  14. }
  15. }
  16. }
  17. public static void IgnoreUnmapped(this IProfileExpression profile)
  18. {
  19. profile.ForAllMaps(IgnoreUnmappedProperties);
  20. }
  21. public static void IgnoreUnmapped(this IProfileExpression profile, Func&lt;TypeMap, bool&gt; filter)
  22. {
  23. profile.ForAllMaps((map, expr) =&gt;
  24. {
  25. if (filter(map))
  26. {
  27. IgnoreUnmappedProperties(map, expr);
  28. }
  29. });
  30. }
  31. public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
  32. {
  33. profile.IgnoreUnmapped((TypeMap map) =&gt; map.SourceType == src &amp;&amp; map.DestinationType == dest);
  34. }
  35. public static void IgnoreUnmapped&lt;TSrc, TDest&gt;(this IProfileExpression profile)
  36. {
  37. profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));
  38. }
  39. }

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> 作为第一个参数。

例如:

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

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:

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

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:

确定