7.投影

AutoMapper有一种自定义映射,叫投影。接下来我们通过一个示例来了解它

7.1示例

//源对象
public class CalendarEvent
{
public DateTime Date { get; set; }
public string Title { get; set; }
} //目标对象
public class CalendarEventForm
{
public DateTime EventDate { get; set; }
public int EventHour { get; set; }
public int EventMinute { get; set; }
public string Title { get; set; }
}

映射说明:把日期拆开分别映射到不同字段

CalendarEvent中属性 说明 CalendarEventForm的属性
Date Date中的年月日-> EventDate
Date Date中小时-> EventHour
Date Date中分钟-> EventMinute

[TestClass]
public class Projection
{
[TestMethod]
public void ProjectionTest()
{
var calendarEvent = new CalendarEvent
{
Date = new DateTime(2008, 12, 15, 20, 30, 0),
Title = "Company Holiday Party"
}; // 初始化映射
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))); CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent); Assert.AreEqual(new DateTime(2008, 12, 15), form.EventDate); // 通过
Assert.AreEqual(20, form.EventHour);// 通过
Assert.AreEqual(30, form.EventMinute);// 通过
Assert.AreEqual("Company Holiday Party", form.Title);// 通过
}
}

AutoMapper之投影的更多相关文章

  1. 04.AutoMapper 之投影(Projection)

    https://www.jianshu.com/p/031553705417 投影(Projection) 投影将源转换为目标而不是扁平化对象模型.如果没有额为的配置AutoMapper需要一个扁平化 ...

  2. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  3. AutoMapper

    什么是AutoMapper? AutoMapper是一个对象和对象间的映射器.对象与对象的映射是通过转变一种类型的输入对象为一种不同类型的输出对象工作的.让AutoMapper有意思的地方在于它提供了 ...

  4. AutoMapper(七)

    返回总目录 Null值替换 如果源类型的成员链上的属性值为Null,Null值替换允许提供一个可替换的值.下面有两个类Person和PersonInfo类,都有一个属性Title(头衔),从Perso ...

  5. AutoMapper的使用

    1.AutoMapper简单介绍 官网:http://automapper.org/ 源码:https://github.com/AutoMapper/AutoMapper NUGET安装: PM&g ...

  6. 1.AutoMapper核心:扁平化

    对象 - 对象映射的一个常见用法是获取一个复杂的对象模型,并将其展开成一个更简单的模型. 您可以采取复杂的模型,如: public class Order { private readonly ILi ...

  7. 0.AutoMapper核心

    AutoMapper是基于约定的对象 - 对象映射器.AutoMapper使用流畅的配置API来定义对象 - 对象映射策略.AutoMapper使用基于约定的匹配算法来匹配源到目标值.AutoMapp ...

  8. AutoMapper使用

    1.安装 现在AutoMapper已经更新到5.0版本了,可查看 http://www.nuget.org/packages/AutoMapper/ 我环境是4.0的,nuget安装 http://w ...

  9. 使用AutoMapper

    一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...

随机推荐

  1. ios 百度地图,火星坐标,地球坐标互转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  2. mysql命令行客户端结果分页浏览

    转载请注明出处:http://xiezhenye.com/2008/06/mysql%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%ae%a2%e6%88%b7%e7%ab%af%e7% ...

  3. Spring AOP 源码分析系列文章导读

    1. 简介 前一段时间,我学习了 Spring IOC 容器方面的源码,并写了数篇文章对此进行讲解.在写完 Spring IOC 容器源码分析系列文章中的最后一篇后,没敢懈怠,趁热打铁,花了3天时间阅 ...

  4. Maximum repetition substring(POJ - 3693)(sa(后缀数组)+st表)

    The repetition number of a string is defined as the maximum number \(R\) such that the string can be ...

  5. TmsHttpClientUtil

    package com.sprucetec.tms.utils; import java.io.IOException;import java.security.GeneralSecurityExce ...

  6. 【TensorFlow】:解决TensorFlow的ImportError: DLL load failed: 动态链接库(DLL)初始化例程失败

    [背景] 在scikit-learn基础上系统结合数学和编程的角度学习了机器学习后(我的github:https://github.com/wwcom614/machine-learning),意犹未 ...

  7. Python小白学习之路(二十四)—【装饰器】

    装饰器 一.装饰器的本质 装饰器的本质就是函数,功能就是为其他函数添加附加功能. 利用装饰器给其他函数添加附加功能时的原则: 1.不能修改被修饰函数的源代码        2.不能修改被修饰函数的调用 ...

  8. web 基础

    web服务器和应用服务器以及web应用框架: web服务器:负责处理http请求,响应静态文件,常见的有Apache,Nginx以及微软的IIS. 应用服务器:负责处理逻辑的服务器.比如php.pyt ...

  9. Winform 多线程--解决界面卡死问题

    public class ThreadInvoker { /// <summary> /// 回调委托 带参数的 /// </summary> /// <param na ...

  10. 利用matlab求图像均值和方差的几种方法

    一.求均值 % 求一副灰度图像的均值 close all; clear; clc; i=imread('d:/lena.jpg'); %载入真彩色图像 i=rgb2gray(i); %转换为灰度图 i ...