开发者

Automapper inheritance -- reusing maps

开发者 https://www.devze.com 2023-03-09 19:12 出处:网络
I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children.

I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children.

For child properties I only want to map the extra fields.

Is this possible? The code I have looks like this

CreateCalculationMap(message)开发者_Go百科;  ---- This does the BASE parent mappping.
    Mapper.CreateMap<QuoteMessage, CalculationGipMessage>()                     -- This is the child.
        .Include<QuoteMessage, CalculationBase>()                               -- Include the parent?
        .ForMember(a => a.OngoingCommission, b => b.MapFrom(c => c.OngoingASF)) - Child
        .ForMember(a => a.SpecialRate, b => b.MapFrom(c => c.BlahBlah)));       - Child

Why does it keep telling me the parents properties are not mapped? Yet, I thought I included them in CreateCalculationMap(message); which contains

Mapper.CreateMap<QuoteMessage, CalculationBase>() ()


FYI I figured this out

 public static IMappingExpression<A, T> ApplyBaseQuoteMapping<A, T>(this   IMappingExpression<A, T> iMappingExpression)
        where A : QuoteMessage
        where T : CalculationGipMessage
    {
        iMappingExpression
            .ForMember(a => a.LoginUserName, b=> b.MapFrom(c => c.LoginUserName))
            .ForMember(a => a.AssetTestExempt, b => b.Ignore())
            ;

        return iMappingExpression;
    }


Mapper.CreateMap<QuoteMessage, CalculationGipMessageChild>()
            .ApplyBaseQuoteMappingToOldCol()
             // do other mappings here


You can't do this I don't think - you need to have the mappings together. This might actually be a bug as the wiki claims that it can be done - however the example given is just relying on the name of the properties to do the mapping, not the includes bit. At least that is how I understand it.

If you look at http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays and change the name of the properties in the Source and Dest (I make them Value 3 and Value4 to really mix things up) then explictly add your mappings.

    Mapper.CreateMap<ChildSource, ChildDestination>()
            .ForMember( x => x.Value4, o => o.MapFrom( y => y.Value2 ) );
        Mapper.CreateMap<ParentSource, ParentDestination>()
            .Include<ChildSource, ChildDestination>()
            .ForMember( x => x.Value3, o => o.MapFrom( y => y.Value1 ) );

Then it seems to fail.

        ChildSource s = new ChildSource()
        {
            Value2 = 1,
            Value1 = 3
        };

        var c = s.MapTo<ChildDestination>();
        var c2 = s.MapTo<ParentDestination>();

        Assert.AreEqual( c.Value3, s.Value1 );
        Assert.AreEqual( c.Value4, s.Value2 );
        Assert.AreEqual( c2.Value3, s.Value1 );
        Assert.AreEqual( c.Value4, s.Value2 );

Other Notes

Also the Include needs to be the child not the parent. The prototype actually states this

    public IMappingExpression<TSource, TDestination> Include<TOtherSource, TOtherDestination>()
        where TOtherSource : TSource
        where TOtherDestination : TDestination

From what I have read you should create your child mapping first, though that might have been an old issue.

Mapper.CreateMap<ChildSource, ChildDest>();

Then your parent

Mapper.CreateMap<ParentSource, Parent Dest>()
       .Include<ChildSource, ChildDest>();

Sourced from http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号