Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automapper, mapping to an interface

Tags:

automapper

I am using automapper (for .net 3.5). Here is an example to illustrate what I am trying to do:

I want to map an A object to a B object. Class definitions:

class A
{
    public I1 MyI { get; set; }

}
class B
{        
    public I2 MyI { get; set; }
}

interface I1
{
    string StringProp1 { get; }
}
interface I2
{
    string StringProp1 { get; }
}

class CA : I1
{
    public string StringProp1
    {
        get { return "CA String"; }
    }
    public string StringProp2 { get; set; }
}
class CB : I2
{
    public string StringProp1
    {
        get { return "CB String"; }
    }
    public string StringProp2 { get; set; }
}

The mapping code:

        A a = new A()
        {
            MyI = new CA()
        };
        // Mapper.CreateMap ...?
        B b = Mapper.Map<A,B>(a);

I want the resulting object b to be populated with an instance of CB. So automapper needs to know that A maps to B, CA maps to CB, and when creating a B populate it's MyI prop with a CB, how do I specify this mapping?

like image 947
Manolo Avatar asked Oct 18 '13 15:10

Manolo


People also ask

Does AutoMapper work with interfaces?

@automapper/pojos : Work with Interfaces/Types along with POJOs. In projects that do not make use of Class, pojos can be used instead.

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

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.


1 Answers

Something like this:

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(x => x.AddProfile<MappingProfile>());

        var a = new A()
        {
            MyI = new CA()
            {
                StringProp2 = "sp2"
            }
        };

        var b = Mapper.Map<A, B>(a);

        Console.WriteLine("a.MyI.StringProp1: " + a.MyI.StringProp1);
        Console.WriteLine("b.MyI.StringProp1: " + b.MyI.StringProp1);

    }
}

>= AutoMapper 2.0.0

public class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<CA, CB>();
        CreateMap<CA, I2>().As<CB>();                                                                       
        CreateMap<A, B>();
    }
}

AutoMapper 1.1.0.188 (.Net 3.5)

public class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<CA, CB>();

        CreateMap<CA, I2>()
            .ConstructUsing(Mapper.Map<CA, CB>)
            ;

        CreateMap<A, B>();
    }
}
like image 148
Martijn B Avatar answered Sep 29 '22 00:09

Martijn B