C# AutoMapper 了解一下
什么是AutoMapper?
简单来说就是将一个对象映射到另一个对象的代码。 摆脱了繁琐的赋值过程 (最常见也就是Model -——ViewModel)
AutoMapper安装
我使用的是VS2015 可以在NuGet中直接输入AutoMapper 去下载

也可以使用控制台命令
PM> Install-Package AutoMapper
这里我定义了两个类 ShopingInfo ShopingInfoViewModel
public class ShopingInfo:EntityBase
{ public string ShopingName { get; set; } public int ShopingCount { get; set; } public decimal ShopingPric { get; set; } public int Stock { get; set; } public int Volumeofvolume { get; set; } public int ShopingTypeId { get; set; } public virtual ShopingType ShopingType { get; set; }
}
public class ShopingInfoViewModel
{
public int ID { get; set; } public string ShopingName { get; set; } public int ShopingCount { get; set; } public decimal ShopingPric { get; set; } public int Stock { get; set; } public int Volumeofvolume { get; set; } public string ShopingTypeName { get; set; }
}
需要用到的命名空间
using AutoMapper;
然后 专门建了一个类用来存放这些映射关系SourceProfile 并且继承了 Profile
public class SourceProfile : Profile
{
public SourceProfile()
{
base.CreateMap<ShopingInfo, ShopingInfoViewModel>();
}
}
如果 我们发现两类中有字段名不一致。
例如 我吧shopingInfoViewModel 中的 ShopingName 改为 Name 那你可以这样写
public class SourceProfile : Profile
{
public SourceProfile()
{
base.CreateMap<ShopingInfo, ShopingInfoViewModel>(); // base.CreateMap<ShopingInfo, ShopingInfoViewModel>().ForMember(x => x.Name,
// q => { q.MapFrom(z => z.ShopingName);
// });
}
}
建了个中间类 用来封装上面的代码
public class AutoMapper
{
public static void Start()
{
Mapper.Initialize(x =>
{
x.AddProfile<SourceProfile>();
});
}
}
然后就在全局类Global中 使得 启动初始化就去加载 加入下面这句代码
AutoMapper.Start(); 好了。 基本的都搞好了。 现在测试一下
可以 看到 已经映射上去了。 学习不能停下。 每天学习点。 会使自己变得越有价值
C# AutoMapper 了解一下的更多相关文章
- 恋爱虽易,相处不易:当EntityFramework爱上AutoMapper
剧情开始 为何相爱? 相处的问题? 女人的伟大? 剧情收尾? 有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易 ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- AutoMapper
什么是AutoMapper? AutoMapper是一个对象和对象间的映射器.对象与对象的映射是通过转变一种类型的输入对象为一种不同类型的输出对象工作的.让AutoMapper有意思的地方在于它提供了 ...
- AutoMapper随笔记
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill 先看效果:(完整Demo:https://git ...
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- AutoMapper的介绍与使用(一)
软件环境 vs2015 asp.net mvc 5 .NET Framework 4.5.2 AutoMapper 5.2.0.0 AutoMapper安装 新建asp.net mvc 项目 Auto ...
- AutoMapper使用中的问题
指定值只会执行一次 public class MomanBaseProfile : Profile { public MomanBaseProfile() { CreateMap<Request ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...
随机推荐
- Linked dylibs built for GC-only but object files built for retain/release for architecture x86_64
编译开源Xcode 插件 SCXcodeSwitchExpander 源码地址: https://github.com/stefanceriu/SCXcodeSwitchExpander 编译环境:X ...
- 转载博客:rabbitmq
原文出处:http://www.cnblogs.com/sam-uncle/p/9202933.html 假设有这一些比较耗时的任务,按照上一次的那种方式,我们要一直等前面的耗时任务完成了之后才能接着 ...
- dede 复制文章,远程图片无法本地化
解决方法: 1.找到dede的后台目录,在后台目录下的inc下找到inc_archives_functions.php 2.搜索GetCurContent函数,找到如下这段代码: preg_match ...
- 转:从框架看PHP的五种境界及各自的薪资待遇(仅限于二三线城市,一线除外)
在撰写此文前首先必须申明的是本人不鄙视任何一种框架,也无意于挑起PHP框架间的战争,更没有贬低某个框架使用者的用意,本文纯粹个人的看法.你可以认为我无知也好,或者装逼也好,请不要试着在任何情况下,随便 ...
- loadrunner12.5-添加检查点
1.点击缩略图资源管理器,找到需要添加检查点的页面. 2.选中需要添加检查点的页面,切换到快照tab,选择“树”型显示方式. 3.在响应窗口下,找到需要添加的检查点的内容,选中--右键--添加文本检查 ...
- 757A Gotta Catch Em' All!
A. Gotta Catch Em' All! time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 论坛遇到附件上传失败问题总结(discuz)
(1)bbs/source/class/class_upload.php 50行左右,注释$attach['target'] $attach['target'] = DISCUZ_ROOT.'./da ...
- 在Jmeter中用JAVA获取Rolling Date
Rolling Date_Weekly import java.util.*; import java.text.SimpleDateFormat; import java.text.DateForm ...
- thrift相关资源
官网资料,具有各语言的例子 https://thrift.apache.org/tutorial/ https://thrift.apache.org/tutorial/py
- TextView等组件的LayoutParams不能随便用,不然组件不显示
TableLayout.LayoutParams lpRow = new TableLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTEN ...
