How to evade writing a lot of repetitive code when mapping?
- by JPCF
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;
}