Biju Thomas Biju Thomas - 3 months ago 52
C# Question

AutoMapper, mapping nested objects of different types

CreateMap<SourceType, DestinationType>()
.ForMember(dest => dest.Type3Property, opt => opt.MapFrom
(
src => new Type3
{
OldValueType5 = src.oldValType6,
NewValueType5 = src.newValType6
}
);


While creating Type3 I have to assign nested properties of Type6 to Type5. How do I do it using Automapper.

Answer

We probably still need a more complete example to fully answer the question. But... from what you've given us so far, I think you only need to add a mapping from Type6 to Type5.

Here's an example of mapping those "nested properties". You can copy past this into a console app to run yourself.

class Program
{
    static void Main(string[] args)
    {
        var config = new MapperConfiguration(cfg =>
        {
            //The map for the outer types
            cfg.CreateMap<SourceType, DestinationType>()
                .ForMember(dest => dest.Type3Property, opt => opt.MapFrom(src => src.Inner));
            //The map for the inner types
            cfg.CreateMap<InnerSourceType, Type3>();
            //The map for the nested properties in the inner types
            cfg.CreateMap<Type6, Type5>()
                //You only need to do this if your property names are different
                .ForMember(dest => dest.MyType5Value, opt => opt.MapFrom(src => src.MyType6Value));

        });

        config.AssertConfigurationIsValid();
        var mapper = config.CreateMapper();

        var source = new SourceType
        {
            Inner = new InnerSourceType {
                OldValue = new Type6 { MyType6Value = 15 },
                NewValue = new Type6 { MyType6Value = 20 }
            }
        };

        var result = mapper.Map<SourceType, DestinationType>(source);
    }
}

public class SourceType
{
    public InnerSourceType Inner { get; set; }
}

public class InnerSourceType
{
    public Type6 OldValue { get; set; }
    public Type6 NewValue { get; set; }
}

public class DestinationType
{
    public Type3 Type3Property { get; set; }
}

//Inner destination
public class Type3
{
    public Type5 OldValue { get; set; }
    public Type5 NewValue { get; set; }
}

public class Type5
{
    public int MyType5Value { get; set; }
}

public class Type6
{
    public int MyType6Value { get; set; }
}