Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper map from inner property to destination class

Tags:

c#

automapper

Cant' seem to figure this one out.

public class DestinationClass
{
    public int InnerPropertyId { get; set; }
    public string StrignValue { get; set; }
}

public class SourceClass
{
    public InnerValue Inner { get; set; }
}

public class InnerValue
{
    public int InnerPropertyId { get; set; }
    public string StrignValue {get;set;}
}

I need to map from SourceClass.InnerValue directly to DestinationClass. How do I do that?

Thanks in advance.

like image 414
b0rg Avatar asked Jan 07 '14 10:01

b0rg


People also ask

Can AutoMapper map enums?

The built-in enum mapper is not configurable, it can only be replaced. Alternatively, AutoMapper supports convention based mapping of enum values in a separate package AutoMapper.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.

What is reverse map in AutoMapper?

The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping. As of now, the mapping we discussed are one directional means if we have two types let's say Type A and Type B, then we Map Type A with Type B.

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping. This test was done on 100,000 records and I must say I was shocked.


2 Answers

As usual, right after I hit post question button:

Mapper.Reset();
// from, to
Mapper.CreateMap<InnerValue, DestinationClass>();
Mapper.CreateMap<SourceClass, DestinationClass>()
    .ConvertUsing(s => Mapper.Map<InnerValue, DestinationClass>(s.Inner));

Mapper.AssertConfigurationIsValid();

var source = new SourceClass() { Inner = new InnerValue() { InnerPropertyId = 123, StringValue = "somethinges" } };

var dest = Mapper.Map<SourceClass, DestinationClass>(source);
like image 173
b0rg Avatar answered Oct 01 '22 02:10

b0rg


We did have a problem with the ConvertUsing not giving the fully populated result because our version of SourceClass had an additional value that we wanted to map to the DestinationClass

We did find that the following code gave the desired result:

{
    ...
    Mapper.CreateMap<InnerValue, DestinationClass>(MemberList.Source);
    Mapper.CreateMap<SourceClass, DestinationClass>(MemberList.Source)
          .ConstructUsing(s => Mapper.Map<DestinationClass>(s.Inner))
          .ForSourceMember(m => m.Inner, opt => opt.Ignore());
    ...
}

public class DestinationClass
{
    public int InnerPropertyId { get; set; }
    public string StringValue { get; set; }
    public string SourceClassValue { get; set; }
}

public class SourceClass
{
    public string SourceClassValue { get; set; }
    public InnerValue Inner { get; set; }
}

public class InnerValue
{
    public int InnerPropertyId { get; set; }
    public string StrignValue {get;set;}
}

ConstructUsing continues to map the remaining members after it has mapped the inner members and ConvertUsing does not.

like image 29
Paul Matovich Avatar answered Oct 01 '22 02:10

Paul Matovich