Pergunta

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;
}
Foi útil?

Solução

I would use code generation to generate the repetitive code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top