简体   繁体   中英

Using Automapper to populate a list in an entity class from a DTO property

I'm very new to using AutoMapper and I've spent a few days googling for a solution with no luck. I am trying to map a DTO, shown below, to my entity class, which contains multiple levels of information (Year,Quarter,Season,Period,Week,Day).

Here's my DTO:

public class CalendarDto
{
    public int Year { get; set; }
    public int Quarter { get; set; }
    public string Season { get; set; }
    public int DayOfWeek { get; set; }
    public string Date { get; set; }
    public int WeekOfYear { get; set; }
    public int DayOfYear { get; set; }
    public int WeekOfPeriod { get; set; }
}

Here's the first two levels of my entity:

    public class Calendar : ICalendar<string>
    {
        public string Id { get; set; }
        public int Year { get; set; }
        public string Date { get; set; }
        public CalendarQuarter Quarters { get; set; }
    }

   public class CalendarQuarter
    {
        public int Year { get; set; }
        public int Quarter { get; set; }
        public string Date { get; set; }
        public List<CalendarSeason> Seasons { get; set; }
    }

As you can see, I have additional levels of nesting from year to quarter, to season, to period, to week and to day. I just provide the first level, year to quarter, because I'm sure once I figure out that one the rest will be straight forward.

I tried with the profile being set to map each class separately, as shown below, from the DTO because all property values match.

public class CalendarProfile : Profile
{
    public CalendarProfile()
    {
        CreateMap<CalendarDto, Calendar>().ReverseMap();
        CreateMap<CalendarDto, CalendarQuarter>().ReverseMap();
        CreateMap<CalendarDto, CalendarSeason>().ReverseMap();
        CreateMap<CalendarDto, CalendarPeriod>().ReverseMap();
        CreateMap<CalendarDto, CalendarWeek>().ReverseMap();
        CreateMap<CalendarDto, CalendarDay>().ReverseMap();
    }
}

When I attempt to map the DTO to the entity in the handler, only the Calendar class is mapped and the CalendarQuarters list is null. It was my understanding that a profile is the same as a config and that all the mapping created in the profile would be executed when Map is executed. Obviously, I'm either wrong, have my mapping set up incorrectly, or both! Lol

var calendar = _mapper.Map<CalendarDto, Calendar>(parms.Dto);

Any suggestions would be greatly appreciated. Thanks for the assistance!

First it is important to mention that using automapper can make mapping look easier, but it is impossible to debug code and you can have problems when your project becomes big and mapping is not thing that is so complicated (you can always map explicitly and debug easily) it is just boring so it is good to have that in mind before you decide to use it, read this articles: article1 , article2 .

You should read documentation if using automapper. I think that you need something like this:

CreateMap<Calendar, CalendarDto>().ForMember(dest => dest.Quarters.Quarter, 
                                             opt => opt.Quarter)
                                  .ForMember(dest => dest.Quarters.Quarter.Seasons.Season
                                             opt => opt.Season);

I removed reverse map because I copied this from my project and changed properties, I hope that you can apply to all properties. Of course you have many properties so you can chain ForMember and define all mappings, if you don't do this mapper is mapping only properties with same name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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