1

There is an example in the Automapper documentation for inheritance as follows:

public class Order { }
public class OnlineOrder : Order { }
public class MailOrder : Order { }

public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }

Mapper.CreateMap<Order, OrderDto>()
    .Include<OnlineOrder, OnlineOrderDto>()
    .Include<MailOrder, MailOrderDto>();
Mapper.CreateMap<OnlineOrder, OnlineOrderDto>();
Mapper.CreateMap<MailOrder, MailOrderDto>();

I want to do something similar but I do not want to use inheritance in the domain model. I am using Entity Framework and I was using inheritance in Entity Framework until I found out that the performance is EXTREMLY bad when you have many derived entities (I have 30). This performance problem is talked about on the net. I want to get rid of the inheritance and use a standard relational model but I can’t figure out what I need to do to get it to work with Automapper.

Modifying the above example, my situation would look like this:

// Domain models in Entity Framework
public class Order
{
  public virtual OnlineOrder OnlineOrder { get; set; }
  public virtual MailOrder MailOrder { get; set; }
}

public class OnlineOrder
{
  public virtual Order Order { get; set; }
}

public class MailOrder 
{ 
  public virtual Order Order { get; set; }
}

// My DTOs
public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }

So you can see that now the domain objects do not use inheritance but simply link to one another like a standard relational database. I would still like the DTO objects to use inheritance though. How would I used Automapper for this example?

Turgrodan
  • 11
  • 4

0 Answers0