简体   繁体   English

在AutoMapper中使用组合图

[英]Use composed map in AutoMapper

Suppose my AutoMapper configuration knows how to map from type T1 to type T2 and from T2 to type T3. 假设我的AutoMapper配置知道如何将T1类型映射为T2,将T2类型映射为T3。 I then have the following. 然后,我有以下内容。 It works: 有用:

public static class MapperExtensions
{
    public static T3 MapVia<T1, T2, T3>(this IMapper mapper, T1 t1) {
        var t2 = mapper.Map<T2>(t1);
        var t3 = mapper.Map<T3>(t2);
        return t3;
    }
    /// <summary> The calling code needs to ensure that t1 is of a type that the mapper knows how to map to type T2.</summary> 
    public static T3 MapVia<T2, T3>(this IMapper mapper, object t1) {
        var t2 = mapper.Map<T2>(t1);
        var t3 = mapper.Map<T3>(t2);
        return t3;
    }
}

My question is whether it is possible to bypass the middle type? 我的问题是是否可以绕过中间类型? I would love to be able to do something in my configuration to tell it "Generate a map from T1 to T3 that is the composition of your map from T1 to T2 and your map from T2 to T3." 我希望能够在配置中进行一些操作以告诉它“生成从T1到T3的地图,这是您从T1到T2的地图以及从T2到T3的地图的组成。” Then I could just map from T1 to T3 normally. 然后我可以正常地从T1映射到T3。

There are times when T2 is large, which may make this a performance issue. 有时T2很大,这可能会导致性能问题。 There are also cases where T2 is not particularly large. 在某些情况下,T2不是特别大。

只是将Lucian Bargaoanu的评论转换为答案:这里没有内置的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM