How to evade writing a lot of repetitive code when mapping?

Posted by JPCF on Stack Overflow See other posts from Stack Overflow or by JPCF
Published on 2010-12-27T23:22:02Z Indexed on 2010/12/28 0:54 UTC
Read the original article Hit count: 219

I have a data access layer (DAL) using Entity Framework, and I want to use Automapper to communicate with upper layers. I will have to map data transfer objects (DTOs) to entities as the first operation on every method, process my inputs, then proceed to map from entities to DTOs. What would you do to skip writing this code?

As an example, see this:

//This is a common method in my DAL
public CarDTO getCarByOwnerAndCreditStatus(OwnerDTO ownerDto, CreditDto creditDto)
{
    //I want to automatize this code on all methods similar to this
    Mapper.CreateMap<OwnerDTO,Owner>();
    Mapper.CreateMap<CreditDTO,Credit>();
    Owner owner = Mapper.map(ownerDto);
    Owner credit = Mapper.map(creditDto)

    //... Some code processing the mapped DTOs

   //I want to automatize this code on all methods similar to this 

   Mapper.CreateMap<Car,CarDTO>();
   Car car = Mapper.map(ownedCar);
   return car;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns