一.   使用意图

常常在开发过程中,碰到一个实体上的属性值,要赋值给另外一个相类似实体属性时,且属性有很多的情况。一般不利用工具的话,就要实例化被赋值实体B,然后再将实体A的字段一个个赋值给B的属性,单单写这些没有技术含量的赋值语句,就要用很大的代码篇幅。假如做得好一点的话,一般就是利用反射的方式,将A属性赋值给B,当然用反射的话,要顺利将A的属性,赋值B的属性,这样确实能够减少代码篇幅,那就要有一些约束或者限制,例如属性名称要相同,属性的数据类型要相同,这样反射起来才不费力。那如何使反射起来,更加灵活,可配置,且配置和反射过程能够分离,实现职责单一,AutoMapper 就是这样一个开源类库。

二.   认识AutoMapper

官方地址 :http://automapper.org/

GitHub 地址:https://github.com/AutoMapper/AutoMapper 包含AutoMapper 源代码与应用Simple。

开发指南:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

从我开发过程使用到一些场景

  1. 实体->实体
  2. 集合->集合
  3. 实体字段名称不同
  4. 实体数据类型不同
  5. 相同名称,相同数据类型无需配置
  6. Queryable Extensions ,也即支持Entity Framework

三.   最佳实践

AutoMapper开发指南,有详细的介绍,我这里就不再搬过说了,大家有空自己研究研究,我这里主要介绍一下AutoMapper比较好的实践方式,废话不多说,直接做项目给大家看。

  1. 项目结构

每个项目用途,解决方案文件夹基本标示清楚。

2. 以订单为例(不是真实业务,只是举个简单的例子),在Models 实体类库 新增OrderModel模型,在ViewModels 新增OrderViewModel模型,代码在下面

using System;
namespace Models
{
public class OrderModel
{
public Guid OrderGuid { get; set; } public string OrderNo { get; set; } public string OrderCreator { get; set; } public DateTime OrderDateTime { get; set; } public string OrderStatus { get; set; } public string Description { get; set; } public string Creator { get; set; } public DateTime CreateDateTime { get; set; } public string LastModifier { get; set; } public DateTime LastModifiedDateTime { get; set; }
}
}
using System;
namespace ViewModels
{
public class OrderViewModel
{
public Guid OrderGuid { get; set; } public string OrderNo { get; set; } public string OrderCreator { get; set; } public DateTime OrderDateTime { get; set; } public string OrderStatus { get; set; } public string Description { get; set; }
}
}

这里假设ViewModel,在使用过程中,不需要创建与修改相关的字段。

3. AutoMapper 配置

通过NuGet 程序包管理器,下载AutoMapper Dll,右键-》AutoMapperProfiles 类库-》管理NuGet程序包-》联机-》右上角搜索“AutoMapper” 下载安装

新增 ModelToViewModelProfile,ViewModelToModelProfile 两个配置类,继承AutoMapper 的 Profile 类,实现Configure重载方法,并分别引入Models & ViewModels 类库,ModelToViewModelProfile,ViewModelToModelProfile 代码如下

using AutoMapper;
using Models;
using ViewModels; namespace AutoMapperProfiles
{
public class ModelToViewModelProfile:Profile
{
protected override void Configure()
{
CreateMap<OrderModel, OrderViewModel>();
}
}
}
using AutoMapper;
using Models;
using ViewModels; namespace AutoMapperProfiles
{
public class ViewModelToModelProfile : Profile
{
protected override void Configure()
{
CreateMap<OrderViewModel, OrderModel>();
}
}
}

4.注册配置

在AutoMapperRegister 项目中,新增AutoMapperProfileRegister 类,按照 第3点,安装一下AutoMapper,同时引用AutoMapperProfiles 类库。代码如下

using AutoMapper;
using AutoMapperProfiles; namespace AutoMapperRegister
{
public class AutoMapperProfileRegister
{
public static void Register()
{
Mapper.Configuration.AddProfile(new ModelToViewModelProfile());
Mapper.Configuration.AddProfile(new ViewModelToModelProfile());
}
}
}

5. 控制台验证是否能够顺利转换

按照 第3点,安装一下AutoMapper,引入 AutoMapperRegister ,Models,ViewModels Dll,编写测试代码,代码如下(见证奇迹的时候到了)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapperRegister;
using Models;
using ViewModels; namespace ConsoleAutoMapperSample
{
class Program
{
static void Main(string[] args)
{
AutoMapperProfileRegister.Register();
var order = new OrderModel
{
OrderGuid = Guid.NewGuid(),
OrderNo = "",
OrderCreator = "david",
OrderDateTime = DateTime.Now,
OrderStatus = "已出库",
Description = "请提供个人发票"
}; var orderView = Mapper.Map<OrderModel, OrderViewModel>(order);
orderView.OrderStatus = "已完成"; var updateOrder = Mapper.Map<OrderViewModel, OrderModel>(orderView);
}
}
}

经过追踪对象属性变化,全部转换成功,不方便截图,稍后我会放出源代码。

最后源代码:

AutoMapperSample.zip

AutoMapper 使用实践的更多相关文章

  1. AutoMapper 最佳实践

    AutoMapper 是一个基于命名约定的对象->对象映射工具. 只要2个对象的属性具有相同名字(或者符合它规定的命名约定),AutoMapper就可以替我们自动在2个对象间进行属性值的映射.如 ...

  2. AutoMapper项目实践

    本次示例,我们单独创建一个 AutoMapperService 的项目,用于放置映射配置文件,映射注册方法,映射公共方法. 1.映射配置文件 用于配置源实体到目标实体的映射 public class ...

  3. AutoMapper用法(转载)

    申明 本文转载自http://www.qeefee.com/article/automapper 作者:齐飞 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前,我 ...

  4. .NET Core中使用AutoMapper

    何为AutoMapper AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 安装AutoMapper 这里我们在NuGet中下载安装Au ...

  5. AutoMapper.Mapper.CreateMap报“System.NullReferenceException: 未将对象引用设置到对象的实例。”异常复现

    >>Agenda: >>Ⅰ.国庆假期问题出现 >>Ⅱ.双休日异常再次出现 >>Ⅲ.排障 >>Ⅳ.异常复盘 >>Ⅴ.修复后监测 & ...

  6. AutoMapper实际项目运用

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...

  7. 配置AutoMapper映射规则《转》

    配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前,我们需要先进行映射规则的配置. public class Source { public int SomeVal ...

  8. AutoMapper用法 转载https://www.cnblogs.com/youring2/p/automapper.html

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...

  9. Open Source

    资源来源于http://www.cnblogs.com/Leo_wl/category/246424.html RabbitMQ 安装与使用 摘要: RabbitMQ 安装与使用 前言 吃多了拉就是队 ...

随机推荐

  1. MyBatis(增删改查)

    1.工程中引入包: mybatis-3.2.7.mysql-connector-java-5.1.22-bin 2.添加配置文件: <?xml version="1.0" e ...

  2. windows下 安装Kali Linux到 U盘的方法

    作者:玄魂工作室 \ 2016年10月20日 把Kali Linux安装到U盘好处很多,可以从U盘启动使用整个电脑的硬件资源, 可以随身携带,减少对自己电脑的影响. 今天要给大家讲的是如何在windo ...

  3. Linux的tree命令

    发现Linux下有个很好的命令,tree,能把目录以树的形式列出来,还支持很强大的参数. 但默认情况下是不带的,得自己去安装,先到这里下载它的代码:http://mama.indstate.edu/u ...

  4. C# Random生成多个不重复的随机数万能接口

    C#,Radom.Next()提供了在一定范围生成一个随机数的方法,我现在有个业务场景是给其他部门推送一些数据供他们做抽样检查处理,假设我的数据库里面有N条数据,现在要定期给其随机推送数据,我需要先拿 ...

  5. ASP.NET Web API标准的“管道式”设计

    ASP.NET Web API的核心框架是一个消息处理管道,这个管道是一组HttpMessageHandler的有序组合.这是一个双工管道,请求消息从一端流入并依次经过所有HttpMessageHan ...

  6. Android-Activity-Dialog theme touch outsize

    最近遇到一个蛋疼的问题: 一个Activity,主题设置成 Dialog 然后点击外面要求这个Activity 不能关闭. 这下好了,直接在 style 的 theme 里面加一个属性就好了. 加上去 ...

  7. Nginx服务器之基础学习

    一.Nginx介绍 nginx:Nginx是一种软件服务器(轻量级),故它最主要的功能就是可以与服务器硬件结合,我们的应用程序可以放在nginx服务器上进行发布,已达到让网民浏览的效果.除此自外,Ng ...

  8. SQL注入

    @org.junit.Test public void testLogin() { CardDAO cd=new CardDAO(); if(cd.dengru("' or 1 = 1--& ...

  9. Sql Server系列:通用表表达式CTE

    1 CTE语法WITH关键字 通用表表达式(Common Table Express, CTE),将派生表定义在查询的最前面.要使用CTE开始创建一个查询,可以使用WITH关键字. CTE语法: WI ...

  10. 网络连接详细信息出现两个自动配置ipv4地址

    问题:网络连接详细信息出现两个自动配置ipv4地址,一个是有效地址,一个是无效地址. 解决办法:先将本地连接ip设置成自动获取,然后点击开始——>运行——>输入cmd,回车,进入命令行界面 ...