我有以下课程结构:

class SrcChild
{
    public bool SomeProperty { get; set; }
}

class SrcParent
{
    public IEnumerable<SrcChild> Children { get; set; }
}


因此SrcParent具有SrcChild对象的集合。

现在,我想将SrcParent的实例映射到DstParent。以下是目标类别:

class DstChild
{
    public bool SomeProperty { get; set; }

    public DstChild(DstParent parent)
    {
        if (parent == null)
            throw new ArgumentNullException();
    }
}

class DstParent
{
    public IEnumerable<DstChild> Children { get; set; }
}


DstParent具有DstChild对象的集合,这些对象使用构造函数注入来保留对其父对象的引用。

使用AutoMapper,我尝试了以下操作:

class Program
{
    static void Main(string[] args)
    {
        /* mapping configuration */
        Mapper.CreateMap<SrcChild, DstChild>()
            .ConstructUsing(
                resolutionContext => new DstChild((DstParent)resolutionContext.Parent.DestinationValue));
        Mapper.CreateMap<SrcParent, DstParent>();

        /* source parent object with two children */
        var srcParent = new SrcParent
        {
            Children = new[] { new SrcChild(), new SrcChild() }
        };

        /* throws an exception */
        var dstParent = Mapper.Map<DstParent>(srcParent);

        Console.ReadKey();
    }
}


这里的主要部分是AutoMapper配置,我在其中尝试从映射上下文中提取对生成的DstParent的引用。这不起作用((DstParent)resolutionContext.Parent.DestinationValue为null),但也许我在这里完全遗漏了一点?

我的另一个想法是使用一个函数来创建子值,如下所示:

class Program
{
    /* Should produce value for DstParent.Children */
    private static IEnumerable<DstChild> MakeChildren(SrcParent src /*, DstParent dstParent */)
    {
        var result = new List<DstChild>();
        // result.Add(new DstChild(dstParent));
        return result;
    }

    static void Main(string[] args)
    {
        /* mapping configuration */
        Mapper.CreateMap<SrcChild, DstChild>();
        Mapper.CreateMap<SrcParent, DstParent>()
            .ForMember(dst => dst.Children,
                opt => opt.MapFrom(src => MakeChildren(src /*, How to obtain a reference to the destination here? */)));

        /* source parent object with two children */
        var srcParent = new SrcParent
        {
            Children = new[] { new SrcChild(), new SrcChild() }
        };

        var dstParent = Mapper.Map<DstParent>(srcParent);

        Console.ReadKey();
    }
}


但是我不知道如何(即使有可能)引用Mapper生成的DstParent对象。

有谁知道如何做到这一点,还是我应该考虑完全放弃该设计并摆脱父级参考文献?提前致谢。

最佳答案

好的,我发现的解决方案并不漂亮,但是可以解决:

class Program
{
    static IEnumerable<DstChild> MakeChildren(IEnumerable<SrcChild> srcChildren, DstParent dstParent)
    {
        var dstChildren = new List<DstChild>();
        foreach (SrcChild child in srcChildren)
        {
            var dstChild = new DstChild(dstParent);
            Mapper.Map(child, dstChild);
            dstChildren.Add(dstChild);
        }
        return dstChildren;
    }

    static void Main(string[] args)
    {
        Mapper.CreateMap<SrcChild, DstChild>();
        Mapper.CreateMap<SrcParent, DstParent>()
            /* Ignore Children property when creating DstParent*/
            .ForMember(dst => dst.Children, opt => opt.Ignore())
            /* After mapping is complete, populate the Children property */
            .AfterMap((srcParent, dstParent) =>
            {
                dstParent.Children = MakeChildren(srcParent.Children, dstParent);
            });

        var source = new SrcParent
        {
            Children = new[]
            {
                new SrcChild() {SomeProperty = true},
                new SrcChild() {SomeProperty = false}
            }
        };

        var destination = Mapper.Map<DstParent>(source);

        Console.ReadKey();
    }
}


目的地已初始化子项,并由AutoMapper正确分配了SomeProperty。如果您找到更好的解决方案,请告诉我。

07-26 06:12