调用IMappingEngine.Map内自定义映射 [英] Calling IMappingEngine.Map inside custom mapping

查看:277
本文介绍了调用IMappingEngine.Map内自定义映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AutoMapper,使用ConvertUsing定义为一个类型,是一个容器自定义映射的时候,我经常需要调用IMappingEngine.Map映射函数内。这是必要的,因为它允许,我重用孩子映射定义

With AutoMapper, when using ConvertUsing to define a custom mapping for a type that is a container, I often need to call IMappingEngine.Map inside the mapping function. This is necessary because it allows-me to reuse the definition of the child mapping.

CreateMap<Order, OrderModel>()
    .ConvertUsing(o => new OrderModel(
        o.Id,
        o.ShippingAddress,
        mapper.Map<IList<OrderItemModel>>(o.Items)
    ));

在为了做到这一点,我需要IMappingEngine的参考。当正在配置映射引擎,我没有对它的引用可以在ConvertUsing论点被捕获。一个简单的解决方案是有一个静态引用它的地方,但我想,以避免它。

In order to do this, I need a reference to IMappingEngine. When the mapping engine is being configured, I don't have a reference to it that can be captured in the ConvertUsing argument. A simple solution is to have a static reference to it somewhere, but I would like to avoid it.

有没有办法去里面目前IMappingEngine参考使用ConvertUsing映射

Is there a way to get a reference to the current IMappingEngine inside a mapping that uses ConvertUsing?

推荐答案

这个答案是基于您的original修订其中包括额外的代码

This answer is based on your original revision which included additional code

如果你看一看在对 Automapper和IOC 他指出文章吉米·博加德以下内容:

If you take a look at the article by Jimmy Bogard on Automapper and IOC he notes the following:

该引擎(MappingEngine),不像我们的配置对象,不需要任何
特殊缓存/寿命行为。该引擎(MappingEngine)很
轻量级的,因为它是真正一堆做有趣的
事情配置方法。最新的引擎(MappingEngine)可单,如果我们想,
,但没有必要。

The MappingEngine, unlike our Configuration object, does not need any special caching/lifetime behavior. The MappingEngine is very lightweight, as it’s really a bunch of methods doing interesting things with Configuration. MappingEngine can be singleton if we want, but there’s no need.

(有更新样本IOC代码Automapper对 github上版本)

(There is updated sample IOC code for the latest version of Automapper on github)

只要你的 ConfigurationStore IConfiguration 独立的,并请求和 IConfigurationProvider 从DI容器的决心,这个单例,文章(和代码示例)主张是好的创建引擎(MappingEngine)注射时。

As long as your ConfigurationStore is a singleton and requests for IConfiguration and IConfigurationProvider from your DI container resolve to this singleton instance, the article (and code examples) advocates that is fine to create new instances of the MappingEngine when injected.

根据上述情况,除了没有注册您的 ConfigurationStore 作为单一实例(我假设,我不熟悉ninject)和不具约束力此实例的 IConfiguration 你的 MappingProfile 的最终实现在你原来的修订实际上是一个可以接受的解决方案。它是确定这是不一样的引擎(MappingEngine)实例。

Based on the above, aside from not registering your ConfigurationStore as a singleton instance (I assume, I'm not familiar with ninject) and not binding this instance to the IConfiguration your final implementation of MappingProfile in your original revision is actually an acceptable solution. It is OK for this to not be the same MappingEngine instance.

不过,在您的样品使用去你的问题可能是值得考虑的情景2 的的文章。如果你没有要求整个应用程序中注入的配置,只有 IMappingEngine ,那么你可以依靠静态映射一流的配置和生命周期管理。总之更改采纳,这将是:

However, going by your sample usage in your question it may be worth considering Scenario 2 in the article. If you have no requirement to inject configuration throughout your application and only the IMappingEngine, then you can rely on the static Mapper class for the configuration and lifetime management. In summary your changes to adopt this would be:


  1. 删除 IConfigurationProvider 在容器中的积聚相关的布线(在 MappingModule )。

  1. Remove the IConfigurationProvider related wiring in the build-up of your container (in the MappingModule).

切换你的 MappingProfile 来使用静态映射

CreateMap<Order, OrderModel>()
    .ConvertUsing(o => new OrderModel(
        o.Id,
        o.ShippingAddress,
        Mapper.Map<IList<OrderItemModel>>(o.Items) //use static Mapper class
));

CreateMap<OrderItem, OrderItemModel>();


  • 添加简介来的映射(也许在 MappingModule ?),并通过映射做任何其他的配置

  • Adding the Profile to the Mapper (perhaps in the MappingModule?), and doing any other configuration through the Mapper:

    Mapper.AddProfile(new MappingProfile());
    


  • 绑定 IMappingEngine 在ninject容器的 Mapper.Engine 属性。

  • Binding IMappingEngine in the ninject container to the Mapper.Engine property.

    这篇关于调用IMappingEngine.Map内自定义映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆