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 ...
随机推荐
- 获取APP的启动图 -Launch Image
http://adad184.com/2015/10/15/tips-access-current-launch-image/
- phpcms中调用外部网站数据
1.在phpcms后台模块->模块管理->数据源->外部数据源 中 添加外部数据源 2.在phpcms前台模板中,使用get标签获取数据源中数据. {pc:get sql=" ...
- uploadify 文件上传报http 302错误
uploadify文件上传会报http 302错误,在配置文件中将处理上传的通用类取消验证, 假设上传的通用处理类是fileUpload.ashx,则在配置文件同添加下面过滤配置能解决问题. < ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- [转]Win7下PEiD已停止工作
转载自:http://www.programlife.net/peid-stop-working-under-win7.html PEID是一个很不错的工具,可查壳,查壳PE信息,借助插件还可以做一些 ...
- Alluxio/Tachyon如何发挥lineage的作用?
在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...
- 连接redis
- wcf服务契约代理链
意图:为了是客户端代理呈现出面向对象的多态的特征 a. 服务端 .契约 实现了契约的继承这个在服务端是一点问题没有,因为oprationcontract可以继承,虽然DataContract不能实现继 ...
- maven 编译的时候总是报一些奇怪的错误 比如 surefire-boot 2.10 .jar 可是私服里查看本来就没有这个高的版本。
或者私服总是 报 read time out , 或者 io 错误, 或者 gzip 解压错误,或者总是尝试下载一些高版本的jar , 而这些jar 可能是不存在的 .. 尝试 重新下载 apac ...
- 8.15jsp document 头部声明 区别
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
