映射时如何逃避编写大量重复代码?
-
13-10-2019 - |
题
我使用实体框架有一个数据访问层(DAL),并且我想使用自动应用程序与上层通信。我将不得不将数据传输对象(DTO)映射到实体,作为每种方法的第一个操作,处理我的输入,然后继续从实体到DTOS映射。您会如何跳过编写此代码?
例如,请参见此处:
//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;
}
解决方案
我将使用代码生成生成重复代码。
不隶属于 StackOverflow