开发者

Complex collections with Automapper

Here is what I have where I hope someone can help us out:

class Source
{  
    string name { get; set; }
    Inner { get; set; }
}

class Inner
{
    Col A { get; set; }
    Col B { get; set; }
}


class Col : IList<ClassX>, IEnunmerable<ClassX>

I need to map class Source to a destination type which has:

class Dest  
{  
    string name { get; set; }  
    IList<ClassY> A { get; set;}  
    IList<ClassY> B { get; set;}  
}  

Now, ClassX and class ClassY share the same properties. ClassY class has a subset开发者_开发知识库 of the ClassX primitive properties with the exact same names and types.

Tried all kinds of mappings. Just the ClassX to ClassY map, with the collections, without and with any mapping get no mapping found between or missing configuration between Source and Dest

AutoMapper.Mapper.Map<Source, Dest>(src);

Can someone help me out with the mapping? Thanks in advance.


This question is a few months old, but if you're still looking for an answer, this is what I tried that worked:

class Source
{
    public string Name { get; set; }
    public Inner Inner { get; set; }
}
class Inner
{
    public Col A { get; set; }
    public Col B { get; set; }
}
class Col : List<ClassX> { }
class ClassX
{
    public int Index { get; set; }
    public string Name { get; set; }

    public ClassX() : this(0, "") { }
    public ClassX(int index, string name)
    {
        this.Index = index;
        this.Name = name;
    }
}
class ClassY
{
    public int Index { get; set; }
    public string Name { get; set; }

    public ClassY() : this(0, "") { }
    public ClassY(int index, string name)
    {
        this.Index = index;
        this.Name = name;
    }
}
class Dest
{
    public string Name { get; set; }
    public List<ClassY> A { get; set; }
    public List<ClassY> B { get; set; }
}

[TestMethod]
public void ComplexTest()
{
    Mapper.CreateMap<Source, Dest>()
        .ForMember(dest => dest.A, config => config.MapFrom(src => src.Inner.A))
        .ForMember(dest => dest.B, config => config.MapFrom(src => src.Inner.B));
    Mapper.CreateMap<ClassX, ClassY>();

    Source source = new Source
        {
            Name = "Source",
            Inner = new Inner
                {
                    A = new Col
                        {
                            new ClassX(1, "First"),
                            new ClassX(2, "Second"),
                            new ClassX(3, "Third"),
                            new ClassX(4, "Fourth"),
                        },
                    B = new Col
                        {
                            new ClassX(5, "Fifth"),
                            new ClassX(6, "Sixth"),
                            new ClassX(7, "Seventh"),
                            new ClassX(8, "Eighth"),
                        },
                }
        };

    Dest destination = Mapper.Map<Source, Dest>(source);

    Assert.AreEqual(source.Name, destination.Name);
    Assert.AreEqual(source.Inner.A.Count, destination.A.Count);
    Assert.AreEqual(source.Inner.B.Count, destination.B.Count);
    Assert.AreEqual(source.Inner.A[0].Name, destination.A[0].Name);
    Assert.AreEqual(source.Inner.B[0].Name, destination.B[0].Name);
}

I didn't go too in-depth with my Asserts, so there may be something I missed, but they appear to be mapped properly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜