3

我对 Automapper 有疑问。我设置了一个测试窗口表单应用程序,下面是代码。还要看每个 MessageBox 后面的注释:

public class FirstClass
    {
        public string FirstProp { get; set; }
        public IList<FirstClassChild> Children { get; set; }
    }

    public class FirstClassChild
    {
        public string FirstChildProp { get; set; }
    }

    public class SecondClass
    {
        public string FirstProp { get; set; }
        public string SecondProp { get; set; }
        public IList<SecondClassChild> Children { get; set; }
    }

    public class SecondClassChild
    {
        public string FirstChildProp { get; set; }
        public string SecondChildProp { get; set; }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            AutoMapper.Mapper.CreateMap<FirstClass, SecondClass>();
            AutoMapper.Mapper.CreateMap<FirstClassChild, SecondClassChild>();

            var f = new FirstClass { FirstProp = "FirstClass" };
            f.Children = new List<FirstClassChild> { new FirstClassChild { FirstChildProp = "FirstClass" } };
            var s = new SecondClass { FirstProp = "SecondClass", SecondProp = "SecondClass" };
            s.Children = new List<SecondClassChild> { new SecondClassChild { FirstChildProp = "SecondClass", SecondChildProp = "SecondClass" } };
            AutoMapper.Mapper.Map(f, s);

            var fc = new FirstClassChild { FirstChildProp = "FirstClass" };
            var sc = new SecondClassChild { FirstChildProp = "SecondClass", SecondChildProp = "SecondClass" };
            AutoMapper.Mapper.Map(fc, sc);

            MessageBox.Show(sc.FirstChildProp);//FirstClass as expected
            MessageBox.Show(sc.SecondChildProp);//SecondClass as expected

            MessageBox.Show(s.FirstProp);//FirstClass as expected
            MessageBox.Show(s.SecondProp);//SecondClass as expected
            MessageBox.Show(s.Children.First().FirstChildProp);//FirstClass as expected
            MessageBox.Show(s.Children.First().SecondChildProp);//Empty not expected!!

        }
    }

我能做些什么来避免这种情况?这种行为是预期的吗?无论如何,任何人都可以指导我如何使 SecondClass childs SecondChildProp 在映射发生之前保持“SecondClass”。

4

1 回答 1

5

我在这里问了一个类似的问题,并在这里找到了另一个类似的问题

我认为@PatrickSteele 提出了一个很好的观点:AutoMapper 应该如何将源列表映射到现有对象的目标列表,而目标列表可能不一定与源列表有任何相似之处?即“但是如果一个列表有 3 个而另一个列表有 5 个呢?

如果您确定FirstClass并且SecondClass具有相同数量的,Children并且如果FirstClass's Nth Child 始终对应于SecondClass's Nth child,则可以尝试以下操作:

Mapper.CreateMap<FirstClass, SecondClass>()
    .ForMember(m => m.Children, o => o.Ignore())
    .AfterMap((src, dest) =>
        {
            for (var i = 0; i < dest.Children.Count; i++)
                Mapper.Map(src.Children[i], dest.Children[i]);
        });

或者如果FirstChildProp是某种唯一键:

Mapper.CreateMap<FirstClass, SecondClass>()
    .ForMember(m => m.Children, o => o.Ignore())
    .AfterMap((src, dest) =>
        {
            foreach (var dChild in dest.Children)
            {
                var sChild = src.Children.Single(c => c.FirstChildProp == dChild.FirstChildProp);
                Mapper.Map(sChild, dChild);
            }
        });
于 2013-01-09T11:39:29.903 回答