简体   繁体   English

业务Object转DTO

[英]Business Object to DTO conversion

I have a list of type Application .我有一个Application类型的列表。 I want to transform that object to an object of type ApplicationDTO .我想将 object 转换为ApplicationDTO类型的 object。 Inside that Application business object, there is a list of Applicants of the type Applicant .Application business object 中,有一个 Applicant 类型的Applicant列表。 Now my DTO has the same list but I am struggling on how to assign the list members of the business object to the list inside the DTO.现在我的 DTO 有相同的列表,但我正在努力研究如何将业务 object 的列表成员分配给 DTO 内的列表。 I have multiple such occurences where it is not known how many items I have in the list.我有多个这样的事件,不知道我在列表中有多少项目。
Here is an example:这是一个例子:

// List of business objects
List<Application> ApplicationList = await _dbContextDigitVP.Applications.FromSqlRaw("Exec dbo.GetApplication {0}", id).ToListAsync();

//DTO object
ApplicationDTO applicationDTO = new ApplicationDTO
{
    ApplicationNumber = Application.ApplicationNumber,
    Country = Application.Country,
    ApplicationUuid = Application.ApplicationUuid,
    PwEmployeeAdUserName = Application.PwEmployeeAdUserName,
    Category = new ApplicationCategoryDTO
    {
        Category = Application.Category?.Category
    },
    Applicants = new List<ApplicantDTO>()
    {
       // add members of business object                       
    }

};

I could go over it with a for loop but is there a way to do this inside the object definition?我可以用 for 循环 go 在它上面,但是有没有办法在 object 定义中做到这一点?

You can use Automapper to map data from one object to another.你可以使用Automapper把map数据从一个object到另一个。

Create Automapper configurations like this for the configure mapping between the ApplicationDTO and Application , ApplicationCategory and ApplicationCategoryDTO and Applicant to ApplicantDTO像这样为ApplicationDTOApplicationApplicationCategoryApplicationCategoryDTO以及ApplicantApplicantDTO之间的配置映射创建 Automapper 配置

var configuration = new MapperConfiguration(cfg => {

  //Source ==> Desti
  //Application and ApplicationDTO classes
  cfg.CreateMap < Application, ApplicationDTO > ()
    .ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category))
    .ForMember(dest => dest.Applicants, opt => opt.MapFrom(src => src.Applicants));
  //ApplicationCategory and ApplicationCategoryDTO classes
  cfg.CreateMap < ApplicationCategory, ApplicationCategoryDTO > ();
  //Applicant and ApplicantDTO classes
  cfg.CreateMap < Applicant, ApplicantDTO > ();
});

In your constructor, you can initialize Mapper like在您的构造函数中,您可以像这样初始化 Mapper

private readonly IMapper _Mapper;

   public TestController(IMapper mapper) {
            _Mapper = mapper;
   }

When you doing the casting you can do somthing like this,当你做选角时,你可以做这样的事情,

var applicationDTOList = _Mapper.Map<List<ApplicationDTO>>(ApplicationList);

You can use AutoMapper .您可以使用AutoMapper

Once you have your types you can create a map for the two types using a MapperConfiguration and CreateMap.获得类型后,您可以使用 MapperConfiguration 和 CreateMap 为这两种类型创建一个 map。 You only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup.通常每个 AppDomain 只需要一个 MapperConfiguration 实例,并且应该在启动期间实例化。

var config = new MapperConfiguration(cfg => cfg.CreateMap<Application, 
ApplicationDTO>());

The type on the left is the source type, and the type on the right is the destination type.左边的类型是源类型,右边的类型是目标类型。 To perform a mapping, call one of the Map overloads:要执行映射,请调用 Map 重载之一:

var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
ApplicationDTO dto = mapper.Map<ApplicationDTO>(Application);

you can also use LINQ to transform objects like without using AutoMapper.您也可以使用 LINQ 像不使用 AutoMapper 一样转换对象。

List<ApplicationDTO> applicationDTOList = ApplicationList.Select(app => new ApplicationDTO
{
    ApplicationNumber = app.ApplicationNumber,
    Country = app.Country,
    ApplicationUuid = app.ApplicationUuid,
    PwEmployeeAdUserName = app.PwEmployeeAdUserName,
    Category = new ApplicationCategoryDTO
    {
        Category = app.Category?.Category
    },
    Applicants = app.Applicants.Select(a => new ApplicantDTO
    {
        // same logic as the above
    }).ToList()
}).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM