有可能通过使用automapper?

时间:2015-11-24 13:17:42

标签: c# automapper

我有一组来自datasource(VSO)的嵌套类,我想映射到合同。映射点是下一个:

  • 映射对象(契约)将有一个名为AreaPathFull的新属性,该属性将具有树中其上方所有节点的连接AreaPath值。

为什么我要这个?

  • 用于在UI上显示
  • 它可以通过处理javascript在UI方面解决,对我来说底层是痛苦的,C#更容易,可能不是最好的想法,但仍然......

问题

  • 可以通过使用Automapper来实现这一目标吗?

不幸的是,我几乎没有使用Automapper的经验,如果不可能,那么我不想花时间使用它。如果是这样,那么我认为,CustomValueResolve将成为我的朋友。

来源

public class Class1ToBeMapped
    {
        public string AreaPath = "Level1";

        public List<Class1ToBeMapped> Children = new List<Class1ToBeMapped>()
        {
            {new Class1ToBeMapped()
            {
                AreaPath = "Level21",
                Children = new List<Class1ToBeMapped>(){}
            }},
            {new Class1ToBeMapped()
            {
                AreaPath = "Level22",
                Children = new List<Class1ToBeMapped>(){}
            }}
        };
    }

映射对象,所需结果:

public class Class2Mapped
    {
        public string AreaPath = "Level1";

        public string AreaPathFull = "Level1";

        public List<Class2Mapped> Children = new List<Class2Mapped>()
        {
            {
                new Class2Mapped()
                {
                    AreaPath = "Level21",
                    AreaPathFull = "Level1/Level21",
                    Children = new List<Class2Mapped>()
                    {
                        {
                            new Class2Mapped()
                            {
                                AreaPath = "Level31",
                                AreaPathFull = "Level1/Level21/Level31",
                                Children = new List<Class2Mapped>()
                                {
                                    {
                                        new Class2Mapped()
                                        {
                                            AreaPath = "Level41",
                                            AreaPathFull = "Level1/Level21/Level31/Level41",
                                            Children = null
                                        }
                                    }
                                }
                            }
                        },
                        {
                            new Class2Mapped()
                            {
                                AreaPath = "Level22",
                                AreaPathFull = "Level1/Level22",
                                Children = new List<Class2Mapped>()
                                    {
                                        new Class2Mapped()
                                        {
                                            AreaPath = "Level32",
                                            AreaPathFull = "Level1/Level22/Level32",
                                            Children = null
                                        }
                                    }
                            }
                        }
                    }
                },

            }
        };
    }

0 个答案:

没有答案