一.   使用意图

常常在开发过程中,碰到一个实体上的属性值,要赋值给另外一个相类似实体属性时,且属性有很多的情况。一般不利用工具的话,就要实例化被赋值实体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. UI控件(UIAlertController)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...

  2. iOS开发系列--IOS程序开发概览

    概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...

  3. Linux sudo

    200 ? "200px" : this.width)!important;} --> 介绍 本篇文章主要介绍sudo配置和用法,为了给某个用户控制权限比如执行某个命令或者关 ...

  4. Atom使用纪要

    一直在更新的原文地址奉上,欢迎PR:Atom使用纪要 官网地址: atom.io 目前(2015/7/29)Atom主题已有725个:Package已有2394 简单交代背景 Atom 是 Githu ...

  5. C语言-指针

    C指针基础知识 C语言中,指针无疑是最令人头疼的.今天无事就来学学C语言的指针,在此留下点笔记,仅供个人参考. 首先要搞懂的是,指针是什么? 指针:是用来存放内存地址的变量. 不管是什么类型的指针,存 ...

  6. fir.im Weekly - 暖心的 iOS 持续集成,你值得拥有

    一则利好消息,flow.ci 支持 iOS 项目持续集成,想试试的伙伴去 Gitter群 问问.首批尝鲜用户@阿米amoy 已经用 flow.ci 实现了基本的 iOS 持续集成,并详细记录整个 Bu ...

  7. Java面试(1)-- Java逻辑运算符

    class Demo04{ public static void main(String[] args){ //逻辑运算符 //例1 System.out.println(true | false & ...

  8. HTML 最简单的tips 怎么支持指定DIV显示提示信息

    <body> <style type="text/css"> a.link{position:relative;} a.link div.tips{ bor ...

  9. MongoDB 创建 Database 和 Collection

    在开始使用MongoDB(Version:3.2.9)之前,必须首先在MongoDB中创建 Database 和 Collection.Database是相互独立的,每个Database都有自己的Co ...

  10. 对Big Table进行全表更新,导致 Replication 同步数据的过程十分缓慢

    在Publisher database中更新一个big table,数据行数是3.4亿多.由于没有更新 clustered Index key,因此,只产生了3.4亿多个Update Commands ...