0

I have a list of types of 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 with how to assign the list members of the business object to the list inside the DTO. I have had multiple such occurrences where it is not known how many items I have on 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 the business object                       
    }

};

I could go over it with a for loop but is there a way to do this inside the object definition?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Florent
  • 111
  • 10
  • 2
    Try https://automapper.org/ – MichaelMao Dec 27 '22 at 07:44
  • Putting this out here without judgment or bias. But I think it is worth to know that others have found tools like automapper to be not without problems ([here](https://gigi.nullneuron.net/gigilabs/pitfalls-of-automapper/), [here](https://ivanazure.wordpress.com/2015/12/02/why-automapping-is-bad-for-you/), [here](https://cezarypiatek.github.io/post/why-i-dont-use-automapper/)). YMMV, but before you commit long-term or large to tools like automapper you should probably read and judge for yourself. – Christian.K Dec 27 '22 at 08:20
  • I searched for like 10 seconds and the first search results were about why not to use automapper - kinda alarming if you ask me – Florent Dec 27 '22 at 08:46
  • `Applicants = Application.Applicants.Select(x => new ApplicantDTO { property initizialization here }).ToList()` – Anton Kovachev Dec 27 '22 at 08:49
  • Check this answer [reusing DTO mapping](https://stackoverflow.com/a/66386142/10646316) – Svyatoslav Danyliv Dec 27 '22 at 13:05

3 Answers3

1

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);
Abbas Aryanpour
  • 391
  • 3
  • 15
0

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);
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
0

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();
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197