Home > Net >  Business Object to DTO conversion
Business Object to DTO conversion

Time:12-27

I have a list of type Application. I want to transform that object to an object of type ApplicationDTO. Inside that Application business object, there is a list of Applicants of the type 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. 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?

CodePudding user response:

You can use AutoMapper.

Once you have your types you can create a map for the two types using a MapperConfiguration and CreateMap. You only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup.

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:

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

CodePudding user response:

You can use Automapper to map data from one object to another.

Create Automapper configurations like this for the configure mapping between the ApplicationDTO and Application, ApplicationCategory and ApplicationCategoryDTO and Applicant to ApplicantDTO

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

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);

CodePudding user response:

you can also use LINQ to transform objects like without using 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();
  • Related