平台之大势何人能挡? 带着你的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.对象映射配置(ConstructUsingForMember 用的比较多)

扩展类:

完整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随笔记的更多相关文章

  1. AutoMapper使用笔记

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

  2. .NET的对象映射工具AutoMapper使用笔记

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

  3. AutoMapper学习笔记

    进入公司后学习到的东西,之前记录在github上 现在搬运过来 AutoMapperDemo 需要安装两个包 AutoMapper AutoMapper.Extensions.Microsoft.De ...

  4. automapper demo

    最近做项目,需要把DataTable中的数据强类型化.于是试用了下比较常用的AutoMapper,通过看代码中附带的Demo与网上的教程,也算能够勉强使用了,现将学习笔记记录如下: namespace ...

  5. WCF服务编程中使用SvcMap实现类型共享等技巧【转】

    原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...

  6. Asp.net core 学习笔记 (AutoMapper)

    参考 : http://www.cnblogs.com/xishuai/p/3700052.html http://www.cnblogs.com/xishuai/p/3704435.html htt ...

  7. .NetCore学习笔记:四、AutoMapper对象映射

    什么是AutoMapper?AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码.这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来 ...

  8. AutoMapper简明教程(学习笔记)

    ].FirstName + nameDto12[].LastName);Console.WriteLine();//Console.ReadKey();//emitMapper error//List ...

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

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

随机推荐

  1. NodeJs之log4js

    log4js log4js是一个管理,记录日志的工具. 其实与morgan的作用类似. 安装 npm install -g log4js log4js的6个日志级别 分别是:trace(蓝色).deb ...

  2. Ignite性能测试以及对redis的对比

    测试方法 为了对Ignite做一个基本了解,做了一个性能测试,测试方法也比较简单主要是针对client模式,因为这种方法和使用redis的方式特别像.测试方法很简单主要是下面几点: 不作参数优化,默认 ...

  3. 超大 Cookie 拒绝服务攻击

    有没有想过,如果网站的 Cookie 特别多特别大,会发生什么情况? 不多说,马上来试验一下: for (i = 0; i < 20; i++) document.cookie = i + '= ...

  4. IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题

    你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的,唯独到了IE10.11这些高版本浏览器就不行了?好吧,这个问题码农连续2天内遇到了2次.那么,我们 ...

  5. mysql学习之 sql语句的技巧及优化

    一.sql中使用正则表达式 select name,email from user where email Regexp "@163[.,]com$"; sql语句中使用Regex ...

  6. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

  7. SQL Server-聚焦在视图和UDF中使用SCHEMABINDING(二十六)

    前言 上一节我们讨论了视图中的一些限制以及建议等,这节我们讲讲关于在UDF和视图中使用SCHEMABINDING的问题,简短的内容,深入的理解,Always to review the basics. ...

  8. 从零开始编写自己的C#框架(28)——建模、架构与框架

    文章写到这里,我一直在犹豫是继续写针对中小型框架的设计还是写些框架设计上的进阶方面的内容?对于中小型系统来说,只要将前面的内容进行一下细化,写上二三十章具体开发上的细节,来说明这个通用框架怎么开发的就 ...

  9. 使用NUnit为游戏项目编写高质量单元测试的思考

    0x00 单元测试Pro & Con 最近尝试在我参与的游戏项目中引入TDD(测试驱动开发)的开发模式,因此单元测试便变得十分必要.这篇博客就来聊一聊这段时间的感悟和想法.由于游戏开发和传统软 ...

  10. TabLayout + ViewPager

    一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...