C#Automapper - 以不同的方式从一个Type中映射具有多个属性的类

时间:2017-12-03 20:52:39

标签: c# automapper

我有一个类验证期,有7个属性来自同一个类:

public class ValidationPeriod
{
    string code;
    DateTime startingDate;
    DateTime endingDate;
    bool validForDaysWithNoTimeSet;
    DayValidation monday;
    DayValidation tuesday;
    DayValidation wednesday;
    DayValidation thursday;
    DayValidation friday;
    DayValidation saturday;
    DayValidation sunday;


    public string Code { get => code; set => code = value; }
    public DateTime StartingDate { get => startingDate; set => startingDate = value; }
    public DateTime EndingDate { get => endingDate; set => endingDate = value; }
    public bool ValidForDaysWithNoTimeSet { get => validForDaysWithNoTimeSet; set => validForDaysWithNoTimeSet = value; }
    public DayValidation Monday { get => monday; set => monday = value; }
    public DayValidation Tuesday { get => tuesday; set => tuesday = value; }
    public DayValidation Wednesday { get => wednesday; set => wednesday = value; }
    public DayValidation Thursday { get => thursday; set => thursday = value; }
    public DayValidation Friday { get => friday; set => friday = value; }
    public DayValidation Saturday { get => saturday; set => saturday = value; }
    public DayValidation Sunday { get => sunday; set => sunday = value; }

    public class DayValidation
    {
        DateTime startingTime;
        DateTime endingTime;
        bool valid;
        bool timeWithinBounds;
        bool endTimeAfterMidnight;

        public DateTime StartingTime { get => startingTime; set => startingTime = value; }
        public DateTime EndingTime { get => endingTime; set => endingTime = value; }
        public bool Valid { get => valid; set => valid = value; }
        public bool TimeWithinBounds { get => timeWithinBounds; set => timeWithinBounds = value; }
        public bool EndTimeAfterMidnight { get => endTimeAfterMidnight; set => endTimeAfterMidnight = value; }
    }

}

我想将它映射到另一个对象,其中日期的定义具有不同的属性。我无法改变第二节课,我不想为一周中的每一天创建单独的课程。

当我尝试这样的事情时,

Automapper给我错误:

 internal static void Map(IMapperConfigurationExpression c)
        {
            c.CreateMap<ODATAConnector.ISWIValidationPeriod, Core.ValidationPeriod>()
                .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.ID))
                .ForMember(dest => dest.StartingDate, opt => opt.MapFrom(src => src.StartingDateTime.Value))
                .ForMember(dest => dest.EndingDate, opt => opt.MapFrom(src => src.EndingDateTime))
                .ForMember(dest => dest.ValidForDaysWithNoTimeSet, opt => opt.MapFrom(src => src.ValidForDaysWithNoTimeSet))
                .ForMember(dest => dest.Monday.Valid, opt => opt.MapFrom(src => src.ValidOnMon))
                .ForMember(dest => dest.Monday.TimeWithinBounds, opt => opt.MapFrom(src => src.Mon_Time_within_Bounds))
                .ForMember(dest => dest.Monday.StartingTime, opt => opt.MapFrom(src => src.Monday_Starting_Time))
                .ForMember(dest => dest.Monday.EndingTime, opt => opt.MapFrom(src => src.Monday_Ending_Time))
                .ForMember(dest => dest.Monday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Mon_End_Time_After_Midnight))

                .ForMember(dest => dest.Tuesday.Valid, opt => opt.MapFrom(src => src.ValidOnTue))
                .ForMember(dest => dest.Tuesday.TimeWithinBounds, opt => opt.MapFrom(src => src.Tue_Time_within_Bounds))
                .ForMember(dest => dest.Tuesday.StartingTime, opt => opt.MapFrom(src => src.Tuesday_Starting_Time))
                .ForMember(dest => dest.Tuesday.EndingTime, opt => opt.MapFrom(src => src.Tuesday_Ending_Time))
                .ForMember(dest => dest.Tuesday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Tue_End_Time_After_Midnight))

                .ForMember(dest => dest.Wednesday.Valid, opt => opt.MapFrom(src => src.ValidOnWed))
                .ForMember(dest => dest.Wednesday.TimeWithinBounds, opt => opt.MapFrom(src => src.Wed_Time_within_Bounds))
                .ForMember(dest => dest.Wednesday.StartingTime, opt => opt.MapFrom(src => src.Wednesday_Starting_Time))
                .ForMember(dest => dest.Wednesday.EndingTime, opt => opt.MapFrom(src => src.Wednesday_Ending_Time))
                .ForMember(dest => dest.Wednesday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Wed_End_Time_After_Midnight))

                .ForMember(dest => dest.Thursday.Valid, opt => opt.MapFrom(src => src.ValidOnThu))
                .ForMember(dest => dest.Thursday.TimeWithinBounds, opt => opt.MapFrom(src => src.Thu_Time_within_Bounds))
                .ForMember(dest => dest.Thursday.StartingTime, opt => opt.MapFrom(src => src.Thursday_Starting_Time))
                .ForMember(dest => dest.Thursday.EndingTime, opt => opt.MapFrom(src => src.Thursday_Ending_Time))
                .ForMember(dest => dest.Thursday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Thu_End_Time_After_Midnight))

                .ForMember(dest => dest.Friday.Valid, opt => opt.MapFrom(src => src.ValidOnFri))
                .ForMember(dest => dest.Friday.TimeWithinBounds, opt => opt.MapFrom(src => src.Fri_Time_within_Bounds))
                .ForMember(dest => dest.Friday.StartingTime, opt => opt.MapFrom(src => src.Friday_Starting_Time))
                .ForMember(dest => dest.Friday.EndingTime, opt => opt.MapFrom(src => src.Friday_Ending_Time))
                .ForMember(dest => dest.Friday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Fri_End_Time_After_Midnight))

                .ForMember(dest => dest.Saturday.Valid, opt => opt.MapFrom(src => src.ValidOnSat))
                .ForMember(dest => dest.Saturday.TimeWithinBounds, opt => opt.MapFrom(src => src.Sat_Time_within_Bounds))
                .ForMember(dest => dest.Saturday.StartingTime, opt => opt.MapFrom(src => src.Saturday_Starting_Time))
                .ForMember(dest => dest.Saturday.EndingTime, opt => opt.MapFrom(src => src.Saturday_Ending_Time))
                .ForMember(dest => dest.Saturday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Sat_End_Time_After_Midnight))

                .ForMember(dest => dest.Sunday.Valid, opt => opt.MapFrom(src => src.ValidOnSun))
                .ForMember(dest => dest.Sunday.TimeWithinBounds, opt => opt.MapFrom(src => src.Sun_Time_within_Bounds))
                .ForMember(dest => dest.Sunday.StartingTime, opt => opt.MapFrom(src => src.Sunday_Starting_Time))
                .ForMember(dest => dest.Sunday.EndingTime, opt => opt.MapFrom(src => src.Sunday_Ending_Time))
                .ForMember(dest => dest.Sunday.EndTimeAfterMidnight, opt => opt.MapFrom(src => src.Sun_End_Time_After_Midnight));

        }

我怎样才能做到这一点。我希望将ValidationPeriod.Monday.StartingTime映射到src.Monday_Starting_Time和ValidationPeriod.Tuesday.StartingTime以映射到src.Tuesday_Starting_Time等。

1 个答案:

答案 0 :(得分:0)

我使用ResolveUsing解决了它。这是我的代码。感谢您初始化我的对象的建议 !

 c.CreateMap<ODATAConnector.ISWIValidationPeriod, Core.ValidationPeriod>()

            .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.ID))
            .ForMember(dest => dest.StartingDate, opt => opt.MapFrom(src => src.StartingDateTime.Value))
            .ForMember(dest => dest.EndingDate, opt => opt.MapFrom(src => src.EndingDateTime))
            .ForMember(dest => dest.ValidForDaysWithNoTimeSet, opt => opt.MapFrom(src => src.ValidForDaysWithNoTimeSet))
            .ForMember(dest => dest.Monday, opt => opt.ResolveUsing(model => new Core.ValidationPeriod.DayValidation()
            {
                Valid = model.ValidOnMon.Value,
                StartingTime = Convert.ToDateTime(model.Monday_Starting_Time),
                EndingTime = Convert.ToDateTime(model.Monday_Ending_Time),
                TimeWithinBounds = model.Mon_Time_within_Bounds.Value,
                EndTimeAfterMidnight = model.Mon_End_Time_After_Midnight.Value
            }))
            .ForMember(dest => dest.Tuesday, opt => opt.ResolveUsing(model => new Core.ValidationPeriod.DayValidation()
            {
                Valid = model.ValidOnTue.Value,
                StartingTime = Convert.ToDateTime(model.Tuesday_Starting_Time),
                EndingTime = Convert.ToDateTime(model.Tuesday_Ending_Time),
                TimeWithinBounds = model.Tue_Time_within_Bounds.Value,
                EndTimeAfterMidnight = model.Tue_End_Time_After_Midnight.Value
            }))