0

I wanna map from flatten object to nested object by using AutoMapper like below source.
How can we do this?

the content of Classes It is preferable to use Class or Property attributes for AutoMapper.

public class Src
{
    public string Value0 { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

[AutoMap(typeof(Src))]
public class Dest
{
    public string Value0 { get; set; }
    public NestedDestChild Child { get; set; }
}

[AutoMap(typeof(Src))]
public class NestedDestChild
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

mapping process (I cut precise process of instantiation of Mapper)

public void ExecuteMapping()
{
    Src srcObj = new Src
    {
        Value0 = "aaa",
        Value1 = "bbb",
        Value2 = "ccc"
    };

    Mapper mapper = new Mapper();
    Dest destObj = mapper.Map<Dest>(srcObj);
    Console.WriteLine(destObj.Value0);       // expected -> aaa
    Console.WriteLine(destObj.Child.Value1); // expected -> bbb
    Console.WriteLine(destObj.Child.Value2); // expected -> ccc
}

I know below source would be compatible, but it is redundancy.
I would like to map only one time from Src to Dest.
So, I wanna execute like above source.

public void ExecuteMapping()
{
    Src srcObj = new Src
    {
        Value0 = "aaa",
        Value1 = "bbb",
        Value2 = "ccc"
    };

    Mapper mapper = new Mapper();
    Dest destObj = mapper.Map<Dest>(srcObj);
    // ↓I wanna cut this line
    NestedDestChild destObj.Child = mapper.Map<NestedDestChild>(srcObj); 
    Console.WriteLine(destObj.Value0);       // expected -> aaa
    Console.WriteLine(destObj.Child.Value1); // expected -> bbb
    Console.WriteLine(destObj.Child.Value2); // expected -> ccc
}
1
  • ForMember(dest => dest.Child, options => options.MapFrom(source => source)) Commented Dec 26, 2020 at 11:30

1 Answer 1

0

You can use AutoMapper from nuget package manager read more about AutoMapper in this link

link to AutoMapper functionality

Here I have tried to make it compatible with the your example

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Src, NestedDestChild>();
        });

        Src srcObj = new Src
        {
            Value1 = "aaa",
            Value2 = "bbb"
        };
        Dest destObj = new Dest();
        IMapper iMapper = config.CreateMapper();

        var destination = iMapper.Map<Src, NestedDestChild>(srcObj);
        destObj.Child = destination;
        Console.WriteLine(destObj.Child.Value1); // expected -> aaa
        Console.WriteLine(destObj.Child.Value2); // expected -> bbb

Needless to say you need to drop AutoMap attribute from above your classes I would recommend creating a manager class which would be responsible for the mapping and for each mapping create a mapping config and return them when you need to map

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. what about if I wanna map also the property from Src to Dest (I added Value0 property in the question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.