自动映射器根据父值映射子对象

时间:2019-04-24 12:34:50

标签: c# automapper

如果标头对象的prop设置为1,则它应该在子级中映射字段type1以键入目标。否则,应使用type2。

如果我可以使用IValueResolver来使用type1或type1extended(如果填充了extended),则奖励点。

这是我的最低可行产品/演示

using AutoMapper;
using AutoMapper.Configuration.Conventions;
using System;
using System.Collections.Generic;

namespace ConsoleAppAutoMapper
{
    class Program
    {
        static void Main(string[] args)
        {
            var source = new SourceParent() {
                Header = new SourceHeader() { Currency = 30, FileName = "testfile.txt", Type = 1 },
                Rows = new List<SourceRow>() {
                    new SourceRow() { ID = 1, Amount1 = 100, Amount2 = 200 },
                    new SourceRow() { ID = 2, Amount1 = 101, Amount2 = 201 },
                    new SourceRow() { ID = 3, Amount1 = 102, Amount2 = 202 }
                } };

            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<SourceParent, DestinationParent>();
                cfg.CreateMap<SourceRow, DestinationRow>()
                    .ForMember(x => x.Type, opt => opt.MapFrom(p => p.Type1));
            });

            var mapper = config.CreateMapper();
            var dest = mapper.Map<DestinationParent>(source);

            Console.WriteLine(dest.Rows[0].Type == 100); // should be true if SourceHeader.Type = 1 and should be 200 (SourceRow.Type2) if SourceHeader.Type = 2
            Console.ReadKey();
        }
    }

    // source

    public class SourceParent
    {
        public SourceHeader Header { get; set; }
        public List<SourceRow> Rows { get; set; }
    }

    public class SourceHeader
    {
        public string FileName { get; set; }
        public int Type { get; set; }
    }

    public class SourceRow
    {
        public int ID { get; set; }
        public int Amount1 { get; set; }
        public int Amount2 { get; set; }
    }

    //destination

    public class DestinationParent
    {
        public DestinationHeader Header { get; set; }
        public List<DestinationRow> Rows { get; set; }
    }

    public class DestinationHeader
    {
        public string FileName { get; set; }
    }

    public class DestinationRow
    {
        public int ID { get; set; }
        public int Type { get; set; }
        public int Amount{ get; set; }  // if type=1 then source is amount1 otherwise amount2
    }
}

修改 我试图通过在sourceparent映射上使用Aftermap来解决此问题,该映射从标头中获取值并将其从destinationrow放入prop中(它是Type值),并希望在行,查看我是否需要道具A或B(type1或type2),但是该aftermap仍然不知道(它为null)它是什么类型,因为它发生在看起来像父对象的aftermap之前。

public class MapRowType : IMappingAction<SourceParent, DestinationParent>
{
    public void Process(SourceParentsource, DestinationParent destination)
    {
        foreach (var row in destination.Rows)
        {
            row.Type = source.Header.Type; // so now I have type in the row, but still do not know if I should use Amount1 or Amount2 
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用分辨率上下文。声明映射:

cfg.CreateMap<SourceRow, DestinationRow>()
            .ForMember(x => x.Type, 
                       opt => opt.ResolveUsing((src, dest1, destMember, resContext) => resContext.Items["Type"] as int? == 1? src.Type2: src.Type1));

传递值后:

var dest = mapper.Map<DestinationParent>(source, opts=> { opts.Items["Type"] = source.Header.Type;});