zoukankan      html  css  js  c++  java
  • 投影

    Projection transform a source to a destination beyond flattening the object model. Without extra configuration, AutoMapper requires a flattened destination to match the source type's naming structure. When you want to project source values into a destination that does not exactly match the source structure, you must specify custom member mapping definitions. For example, we might want to turn this source structure:

        public class CalendarEvent
        {
        	public DateTime Date { get; set; }
        	public string Title { get; set; }
        }
    

    Into something that works better for an input form on a web page:

        public class CalendarEventForm
        {
        	public DateTime EventDate { get; set; }
        	public int EventHour { get; set; }
        	public int EventMinute { get; set; }
        	public string Title { get; set; }
        }
    

    Because the names of the destination properties do not exactly match the source property (CalendarEvent.Date would need to be CalendarEventForm.EventDate), we need to specify custom member mappings in our type map configuration:

        // Model
        var calendarEvent = new CalendarEvent
        	{
        		Date = new DateTime(2008, 12, 15, 20, 30, 0),
        		Title = "Company Holiday Party"
        	};
        
        // Configure AutoMapper
        Mapper.Initialize(cfg => 
          cfg.CreateMap<CalendarEvent, CalendarEventForm>()
        	.ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date))
        	.ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))
        	.ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute)));
        
        // Perform mapping
        CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);
        
        form.EventDate.ShouldEqual(new DateTime(2008, 12, 15));
        form.EventHour.ShouldEqual(20);
        form.EventMinute.ShouldEqual(30);
        form.Title.ShouldEqual("Company Holiday Party");
    

    Each custom member configuration uses an action delegate to configure each individual member. In the above example, we used the MapFrom option to perform custom source-to-destination member mappings. The MapFrom method takes a lambda expression as a parameter, which is then evaluated later during mapping. The MapFrom expression can be any Func<TSource, object> lambda expression.

  • 相关阅读:
    csrf攻击 xss区别
    同源策略
    JavaScript中数组的排序——sort()
    箭头函数
    bind(),call(),apply()
    异步操作
    slice(), splice(),split(),indexOf(),join(),replace()
    04-Linux系统编程-第01天(文件IO、阻塞非阻塞)
    03-Linux命令基础-第03天(makefile、静态库、动态库、gdb调试工具)
    02-Linux命令基础-第02天(压缩包管理、服务器搭建与使用、vim)
  • 原文地址:https://www.cnblogs.com/Leman/p/5774363.html
Copyright © 2011-2022 走看看