简体   繁体   English

如何使用自动映射器映射对象,但目标对象根据配置看起来不同?

[英]Howto to map an object with automapper but have the destination object look different depending on configuration?

I'm convinced there is a simple solution for my problem but I'm not seeing it, so I need some help in lighting me the way.我相信我的问题有一个简单的解决方案,但我没有看到它,所以我需要一些帮助来照亮我的道路。

We use automapper to map our internal objects into fhir objects to communicate with different systems.我们使用 automapper 将我们的内部对象映射到 fhir 对象以与不同的系统进行通信。 Now one of the systems has been extended so that it needs the same fhir object but slightly different in the content.现在其中一个系统已经扩展,因此它需要相同的 fhir 对象,但内容略有不同。 That means if our system is configured to use the old version, the fhir object has to look like as before.这意味着如果我们的系统配置为使用旧版本,则 fhir 对象必须看起来像以前一样。 If the system has been configured to support the new version, the fhir object has to be filled with other content to meet the new system requirements.如果系统已经配置为支持新版本,则fhir对象必须填充其他内容以满足新系统要求。

In the profile class it is not possible to work with external configuration, so filling the object depending on my system configuration is not possbile there (as fas as I know).在配置文件类中,无法使用外部配置,因此不可能根据我的系统配置填充对象(据我所知)。 The objects to map have not changed, only the content has, so adding different maps to the automapper configuration won't work as the implementations do not differ for automapper.要映射的对象没有改变,只有内容改变了,所以将不同的映射添加到自动映射器配置将不起作用,因为自动映射器的实现没有区别。 I tried Before- and AfterMapping-action but run into errors saying maps are missing.我尝试了 Before- 和 AfterMapping-action,但遇到错误说地图丢失了。 So what's the best way to achive this?那么实现这一目标的最佳方法是什么?

Let's assume you would use IConfiguration to detect which version your system is configured to use.假设您将使用IConfiguration来检测您的系统配置为使用哪个版本。

You can pass this information into the ResolutionContext that you can access it in your mapper configuration.您可以将此信息传递到您可以在映射器配置中访问它的ResolutionContext

public class Service
{
    private readonly IConfiguration configuration;
    private readonly IMapper mapper;

    public Service(IConfiguration configuration, IMapper mapper)
    {
        this.configuration = configuration;
        this.mapper = mapper;
    }

    public DoSomething()
    {
        SourceClass source = new();

        DestinationClass destination = mapper.Map<DestinationClass>(
            source,
            // Here we pass the info whether to use the old or the new version.
            options =>
            options.Items["UseNewVersion"] =
                configuration.GetValue<bool>("UseNewVersion")
        );
    }
}

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<SourceClass, DestinationClass>()
            .ForMember(
                destination => destination.Property,
                (source, destination, destinationMember, context) =>
                {
                    // Here we get whether to use the old or the new version.
                    bool useNewVersion = (bool)context.Items["UseNewVersion"];

                    return useNewVersion ? "NEW" : "OLD";
                }
            );
    }
}

Both of the Items properties are of type Dictionary<string, object> .这两个Items属性都是Dictionary<string, object>类型。

  • IMappingOperationOptions.Items
  • ResolutionContext.Items

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当目标 object 在 Automapper 中没有值时,如何 map - how to map only if destination object doesn't have a value in Automapper AutoMapper将对象映射到任何继承的目标类型 - AutoMapper map object to any inherited destination type 如何使用AutoMapper将目标对象与源对象中的子对象进行映射? - How to use AutoMapper to map destination object with a child object in the source object? AutoMapper将源对象上的单个列表映射到目标对象上的两个列表 - AutoMapper Map single list on source object into two lists on destination object AutoMapper如何将对象A映射到对象B,这取决于上下文 - AutoMapper How To Map Object A To Object B Differently Depending On Context 如何使用自动映射器将对象映射到未知的目标类型? - How can I use Automapper to map an object to an unknown destination type? Automapper - 如何从源子对象映射到目标 - Automapper - How to map from source child object to destination Automapper-如何将两个复杂的集合映射到一个目标集合对象 - Automapper - how to map two complex collections to one destination collection object Automapper 配置文件将具有嵌套列表的对象映射到目标列表 - Automapper profile to map object with nested list to destination list AutoMapper - 使用相同的源和目标对象类型映射 - AutoMapper - Map using the same source and destination object types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM