2

我有一个使用实体框架的数据访问层 (DAL),我想使用 Automapper 与上层通信。作为每个方法的第一个操作,我必须将数据传输对象 (DTO) 映射到实体,处理我的输入,然后继续从实体映射到 DTO。你会怎么做才能跳过编写这段代码?

例如,请参见:

//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;
}
4

1 回答 1

1

我会使用代码生成来生成重复代码。

于 2010-12-27T23:27:16.030 回答