JP Hellemons
JP Hellemons

Reputation: 6057

Automapper map child object based on parent value

If the header object has a prop set to 1 then it should map field type1 in the child to type in the destination. Otherwise it should use type2.

Bonus points if I can use IValueResolver to use type1 or type1extended if extended is filled.

Here is my minimum viable product/demo

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
    }
}

edit I tried to solve it by having an Aftermap on the sourceparent mapping which took the value from the header and put it in a prop from the destinationrow (it is the Type value) and wanted another aftermap on the row to see if I needed prop A or B (type1 or type2) but that aftermap still does not know (it's null) what type it is because it happens before the aftermap of the parent it seems.

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 
        }
    }
}

Upvotes: 0

Views: 3022

Answers (1)

asd
asd

Reputation: 904

you can use the resolution context. Declare the mapping:

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

After pass the value:

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

Upvotes: 2

Related Questions