简体   繁体   中英

Configuring AutoMapper with Lists and Enums

I'm currently configuring AutoMapper as I need to convert a list of Person(s) to a list of PersonViewModel(s). I have flattened out the Repository for my ViewModel , see below:

public class Person
{
    public int PersonId { get; set; }        
    public string Name { get; set; }        
    public List<CalendarEntry> Days { get; set; }
}
public class CalendarEntry
{
    public int CalendarEntryId { get; set; }
    public int PersonId { get; set; }    
    public Days Day { get; set; }    
    public DateTime From { get; set; }
    public DateTime To { get; set; }  
}
public class PersonViewModel
{
    public int PersonId { get; set; }   
    public string PersonName { get; set; }

    public bool IsMonday { get; set; }
    public bool IsTuesday { get; set; }
    public bool IsWednesday { get; set; }
    public bool IsThursday { get; set; }
    public bool IsFriday { get; set; }

    public DateTime? MondayFrom { get; set; }        
    public DateTime? MondayTo { get; set; }        
    public DateTime? TuesdayFrom { get; set; }        
    public DateTime? TuesdayTo { get; set; }        
    public DateTime? WednesdayFrom { get; set; }        
    public DateTime? WednesdayTo { get; set; }        
    public DateTime? ThursdayFrom { get; set; }        
    public DateTime? ThursdayTo { get; set; }        
    public DateTime? FridayFrom { get; set; }        
    public DateTime? FridayTo { get; set; }     
}

I have created an AutoMapperConfig file where I have Created a Map for each member like so:

AutoMapper.Mapper.CreateMap<Person, PersonViewModel>()
            .ForMember(dest => dest.PersonId, opts => opts.MapFrom(src => src.PersonId))
            .ForMember(dest => dest.PersonName, opts => opts.MapFrom(src => src.Name))

Now, I have reached the CalendarEntryId and obviously this is contained within the List<CalendarEntry> MeetingDays prop, how do I handle this property and also the 5 bools for Mon-Fri, when the user creates the record from the front end they are selecting check boxes and entering a time field, the rest of the DateTime (day/month/year) is being set to 01/01/1900 in the database. Days is an Enum with Mon-Sun stored so I am able cross reference somehow to check whether for example IsMonday should be true or false, but I'm getting a tad confused with how to configure this part of AutoMapper up?

How do you expect to map a list of ids to one int property CalendarEntryId ?

If I was you, I would change my view model to contain a collection of CalendarEntryViewModel 's.

Create a map CreateMap<CalendarEntry, CalendarEntryViewModel> and then Automapper would map your List<CalendarEntry> to your List<CalendarEntryViewModel> on your view model.

Also, since your data model and view model have the same property name for PersonId you don't need to explicitly map that. Automapper will do this for you.

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