1

I have a hierarchy of 12 entity framework objects.

I have also for each entity a DTO created.

I want to send the DTOs over the wire.

I have to use the DTO-approach.

How would you map this amount of objects with Automapper?

Do I have to use 12 times the AutoMapper.Map method?

UPDATE

I get this error now:

{"Missing type map configuration or unsupported mapping.\r\n\r\n....
I have an NumberEntity.cs with 3 complex properties which I want to map to

a NumberDTO.cs with 3 complex properties.

Is that not possible? Do I have to setup an extra mapping for complex classes within a class?

Pascal
  • 12,265
  • 25
  • 103
  • 195

2 Answers2

2

If you have inheritance hierarchy then use this approach https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance. You need to register mapping for each pair of types and call .Map only once. If you have nested objects, then use this one https://github.com/AutoMapper/AutoMapper/wiki/Nested-mappings. Again, only one .Map call. If you posted some examples of your object hierarchy it would be easier to tell.

To sum up, you have to have mapping for each 'complex' type.

Andrey Stukalin
  • 5,328
  • 2
  • 31
  • 50
0

No, you have to create a mapping for each DTO in config.

Let's say you have a Person.cs, Address.cs, Car and User in your Entities

public class User
{
public int UserId {get;set;}
public string UserName {get;set;}

public PersonDTO Person {get;set;}
}

public class Person
{
public int PersonID {get;set;}
public string Name {get;set;}

public Address Address {get;set;}
public Car Car {get; set;}

}

So you also have PersonDTO, AddressDTO, and CarDTO

e.g.

    public class UserDTO
    {
    public int UserId {get;set;}
    public string UserName {get;set;}

// HERE IF YOU HAVE:
// public PersonDTO MyPerson {get;set;}
// IT WILL NOT MAP
// Property Names should match

    public PersonDTO Person {get;set;}

    }


public class PersonDTO
{
   public int PersonID {get;set;}
   public string Name {get;set;}

   public AddressDTO Address {get;set;}
   public CarDTO Car {get;set;}

}

Your class where you have all Mappings defined.

public class MapperConfig
{
  public static void CreateMappings()
  {
        Mapper.CreateMap<UserDTO, Entities.User>().ReverseMap();
        Mapper.CreateMap<PersonDTO, Entities.Person>().ReverseMap();

        Mapper.CreateMap<AddressDTO, Entities.Address>().ReverseMap();
        Mapper.CreateMap<CarDTO, Entities.Car>().ReverseMap();
  }
}

then in you code:

UserDTO user = Mapper.Map<UserDTO>(context.Users.First(s => s.UserId == 1));

To Map a List:

List<UserDTO> users = Mapper.Map<IEnumerable<UserDTO>>(context.Users).ToList();

As long as the Name of the Properties are the same they should map.

Dawood Awan
  • 7,051
  • 10
  • 56
  • 119
  • Does reverseMap mean it is configured in both directions? – Pascal May 22 '15 at 13:32
  • This is the Syntax: AutoMapper.Mapper.CreateMap(); http://cpratt.co/using-automapper-creating-mappings/ – Dawood Awan May 22 '15 at 13:33
  • Oh I want that but... the reverse mapping is done on the other side , the wcf server. Does the reverse still work? :P – Pascal May 22 '15 at 13:34
  • I am not sure, I think you will have to create the mappings both on Client and Server side. Or put it in a Common Library and share between client and server – Dawood Awan May 22 '15 at 13:35