Картографиране на дъщерни класове с родител, инжектиран в конструктора с помощта на AutoMapper

Имам следната структура на класа:

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();
    }
}

но не знам как (ако изобщо е възможно) да получа препратка към обекта DstParent, произведен от Mapper.

Някой има ли идея как да направи това или по-скоро трябва да помисля да премахна напълно този дизайн и да се отърва от препратката към родител? Благодаря предварително.


person Piotr    schedule 15.12.2015    source източник


Отговори (1)


Добре, решението, което намерих, не е красиво, но работи:

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();
    }
}

Дестинацията има инициализирани деца, като SomeProperty е правилно зададен от AutoMapper. Моля, уведомете ме, ако намерите по-добре изглеждащо решение.

person Piotr    schedule 15.12.2015