简体   繁体   English

使用列表和枚举配置AutoMapper

[英]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). 我当前正在配置AutoMapper,因为我需要将Person列表转换为PersonViewModel列表。 I have flattened out the Repository for my ViewModel , see below: 我已经将ViewModel的存储库展平了,如下所示:

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: 我创建了一个AutoMapperConfig文件,在其中为每个成员创建了一个地图,如下所示:

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. 现在,我到达了CalendarEntryId,显然它包含在List<CalendarEntry> MeetingDays道具中,当用户从他们选择的前端创建记录时,如何处理此属性以及Mon-Fri的5个布尔值复选框并输入时间字段,则数据库中其余的DateTime (日/月/年)被设置为01/01/1900 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? Days是一个存储有Mon-Sun的Enum ,因此我可以通过某种方式进行交叉引用来检查IsMonday是否为true或false,但是我对如何配置AutoMapper这一部分感到IsMonday困惑?

How do you expect to map a list of ids to one int property CalendarEntryId ? 您如何期望将ID列表映射到一个int属性CalendarEntryId

If I was you, I would change my view model to contain a collection of CalendarEntryViewModel 's. 如果我是你,我将更改我的视图模型以包含CalendarEntryViewModel的集合。

Create a map CreateMap<CalendarEntry, CalendarEntryViewModel> and then Automapper would map your List<CalendarEntry> to your List<CalendarEntryViewModel> on your view model. 创建一个映射CreateMap<CalendarEntry, CalendarEntryViewModel> ,然后Automapper会将您的List<CalendarEntry>映射到您的视图模型上的List<CalendarEntryViewModel>

Also, since your data model and view model have the same property name for PersonId you don't need to explicitly map that. 另外,由于您的数据模型和视图模型具有与PersonId相同的属性名称,因此无需显式映射该名称。 Automapper will do this for you. Automapper将为您完成此任务。

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

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