开发者

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

开发者 https://www.devze.com 2023-02-01 03:47 出处:网络
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 operatio

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
开发者_Go百科    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;
}


I would use code generation to generate the repetitive code.

0

精彩评论

暂无评论...
验证码 换一张
取 消