AutoMapper随笔记
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill
先看效果:(完整Demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/05.AutoMapper)
原来是这么干的:

有了AutoMapper是这么干的:

————————————————————
1.AutoMapper初始化

2.配置文件

3.对象映射配置(ConstructUsing 和 ForMember 用的比较多)

扩展类:



完整Demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/05.AutoMapper
把扩展类贴一下吧:
/// <summary>
/// AutoMapper扩展类
/// </summary>
public static class AutoMapperExt
{
#region 由AutoMapper创建目标对象
/// <summary>
/// 类型映射-由AutoMapper创建目标对象
/// </summary>
public static T MapTo<T>(this object obj)
{
if (obj == null) return default(T);
return Mapper.Map<T>(obj);
} /// <summary>
/// 集合列表类型映射-由AutoMapper创建目标对象
/// </summary>
public static IEnumerable<TDestination> MapToList<TDestination>(this IEnumerable source)
{
if (source == null) return new List<TDestination>();
return Mapper.Map<IEnumerable<TDestination>>(source);
}
#endregion #region 把源对象中的属性值合并/覆盖到目标对象
/// <summary>
/// 类型映射-把源对象中的属性值合并/覆盖到目标对象
/// </summary>
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination) where TSource : class where TDestination : class
{
if (source == null) return destination;
return Mapper.Map(source, destination);
} /// <summary>
/// 集合列表类型映射-把源对象中的属性值合并/覆盖到目标对象
/// </summary>
public static IEnumerable<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source, IEnumerable<TDestination> destination)
{
return Mapper.Map(source, destination);
}
#endregion #region DataReader映射
/// <summary>
/// DataReader映射
/// </summary>
public static IEnumerable<T> DataReaderMapTo<T>(this System.Data.IDataReader reader)
{
Mapper.Initialize(cfg => cfg.CreateMap<System.Data.IDataReader, IEnumerable<T>>());
return Mapper.Map<System.Data.IDataReader, IEnumerable<T>>(reader);
}
#endregion
}
AutoMapper随笔记的更多相关文章
- AutoMapper使用笔记
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...
- .NET的对象映射工具AutoMapper使用笔记
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...
- AutoMapper学习笔记
进入公司后学习到的东西,之前记录在github上 现在搬运过来 AutoMapperDemo 需要安装两个包 AutoMapper AutoMapper.Extensions.Microsoft.De ...
- automapper demo
最近做项目,需要把DataTable中的数据强类型化.于是试用了下比较常用的AutoMapper,通过看代码中附带的Demo与网上的教程,也算能够勉强使用了,现将学习笔记记录如下: namespace ...
- WCF服务编程中使用SvcMap实现类型共享等技巧【转】
原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...
- Asp.net core 学习笔记 (AutoMapper)
参考 : http://www.cnblogs.com/xishuai/p/3700052.html http://www.cnblogs.com/xishuai/p/3704435.html htt ...
- .NetCore学习笔记:四、AutoMapper对象映射
什么是AutoMapper?AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码.这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来 ...
- AutoMapper简明教程(学习笔记)
].FirstName + nameDto12[].LastName);Console.WriteLine();//Console.ReadKey();//emitMapper error//List ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
随机推荐
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- ASP.NET路由模型解析
大家好,我又来吹牛逼了 ~-_-~ 转载请注明出处:来自吹牛逼之<ASP.NET路由模型解析> 背景:很多人知道Asp.Net中路由怎么用的,却不知道路由模型内部的运行原理,今天我就给大家 ...
- MSDN文档篇
很多人网上下载3~10G不等的MSDN文档,发现,下载完成了不会用 很多人每次都得在线下载文档,手上万千PC,都重新下载不是得疯了? so==> 先看几张图 推荐一个工具:https://vsh ...
- ExtJS 4.2 介绍
本篇介绍ExtJS相关知识,是以ExtJS4.2.1版本为基础进行说明,包括:ExtJS的特点.MVC模式.4.2.1GPL版本资源的下载和说明以及4种主题的演示. 目录 1. 介绍 1.1 说明 1 ...
- System.FormatException: GUID 应包含带 4 个短划线的 32 位数(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。
在NHibernate数据库查询中出现了这个错误,由于是数据库是mysql的,当定义的字段为char(36)的时候就会出现这个错误. [解决方法] 将char(36) 改成varchar(40)就行了 ...
- 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?
引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...
- 一道返回num值的小题目
题目描述: 实现fizzBuzz函数,参数num与返回值的关系如下: .如果num能同时被3和5整除,返回字符串fizzbuzz .如果num能被3整除,返回字符串fizz .如果num能被5整除,返 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- 关于VS2015 ASP.NET MVC添加控制器的时候报错
调试环境:VS2015 数据库Mysql WIN10 在调试过程中出现类似下两图的同学们,注意啦. 其实也是在学习的过程中遇到这个问题的,找了很多资料都没有正面的解决添加控制器的时候报错的问题,还是 ...
- python基础
内容概要: 一.python2 or python3 目前大多使用python2.7,随着时间的推移,python3将会成为python爱好者的主流. python2和3区别: 1.PRINT IS ...