开发者

Mapping List of objects from parent class using AutoMapper

开发者 https://www.devze.com 2023-02-02 18:56 出处:网络
I have following classes: public class TestClass { public int ID { get; set; } public string Name { get; set; }

I have following classes:

    public class TestClass
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class ParentSource
    {
        public int A { get; set; }
        public List<TestClass> LstSource { get; set; }
    }

    public class ChildSource : ParentSource
    {
        public int B { get; set; }
    }

    public class ParentDestination
    {
        public int A { get; set; }
        public List<TestClass> LstDestination { get; set; }
    }

    public class ChildDestination : ParentDestination
    {
        public int B { get; set; }
    }

I'm trying to map ChildSource to ChildDestination using AutoMapper version 1.1.0.188.

Mapper.Initialize(cfg =>
{  
       cfg.CreateMap<ParentSource, ParentDestination>()
       .Include<ChildSource, ChildDestination>()
       .ForMember(dest => dest.LstDestination, opt => opt.MapFrom(src => src.LstSource))            
       .ForMember(dest => dest.A, opt => opt.MapFrom(src => src.A));


    cfg.CreateMap<开发者_运维百科ChildSource, ChildDestination>() 
       .ForMember(dest => dest.B, opt => opt.MapFrom(src => src.B));
});

Then I do a test:

ChildSource test = new ChildSource();
test.LstSource = new List<SourceTestClass>() { new SourceTestClass() { ID = 3, Name = "abc" }, new SourceTestClass() { ID = 11, Name = "xyz" } };
test.A = 100;
test.B = 200;

ChildDestination result = Mapper.Map<ChildSource, ChildDestination>(test);

Object result contains proper A and B values, but LstSource is null. What am I doing wrong?


I answered this one here Automapper inheritance -- reusing maps

...snip

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

精彩评论

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