AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器
本篇AutoMapper使用场景:
※ 源字典集合转换成目标字典集合
※ 枚举映射
※ 自定义解析器
※ 源中的复杂属性和Get...方法转换成目标属性
源字典集合转换成目标字典集合
□ Domain model
public class SourceValue
{
public int Value { get; set; }
}
□ View model
public class DestValue
{
public int Value { get; set; }
}
□ 映射配置
Mapper.CreateMap<SourceValue, DestValue>();
□ 使用
public ActionResult Dic()
{
var sourceDict = new Dictionary<string, SourceValue>
{
{"First", new SourceValue(){Value = 5}},
{"Second",new SourceValue(){Value = 10}}
};
var destDict = Mapper.Map<Dictionary<string, SourceValue>, IDictionary<String, DestValue>>(sourceDict);
return View((destDict));
}
枚举映射
public enum OrderStatus : short
{
InProgress = 0,
Complete = 1
}
public enum OrderStatusDto
{
InProgress = 0,
Complete = 1
}
□ 使用
//例子1
public ActionResult Meiju()
{
var dest = Mapper.Map<OrderStatus, OrderStatusDto>(OrderStatus.InProgress);
return View(dest);
}
//例子2
public ActionResult Meiju1()
{
var dest =
Mapper.Map<AttributeTargets, AttributeTargets>(AttributeTargets.Class | AttributeTargets.Interface);
return View(dest);
}
□ 要点
枚举映射不需要做映射配置。
自定义解析器
□ Domain model
public class Source1
{
public int Value { get; set; }
public int Value2 { get; set; }
}
□ View model
public class Destination1
{
public int Total { get; set; }
}
□ 自定义解析器,继承于ValueResolver<,>
public class CustomResolver : ValueResolver<Source1,int>
{
protected override int ResolveCore(Source1 source)
{
return source.Value + source.Value2;
}
}
□ 映射配置
Mapper.CreateMap<Source1, Destination1>()
.ForMember("Total", opt => opt.ResolveUsing<CustomResolver>());
□ 使用
public ActionResult Jiexi()
{
var source = new Source1()
{
Value = 5,
Value2 = 7
};
var dest = Mapper.Map<Source1, Destination1>(source);
return View(dest);
}
□ 要点
派生ValueResolver<,>实现自定义解析器,实现对源属性的"计算" 转换成目标属性。
源中的复杂属性和Get...方法转换成目标属性
□ Domain model
public class Order2
{
private readonly IList<OrderLineItem2> _orderLineItems = new List<OrderLineItem2>();
public Customer2 Customer { get; set; }
public OrderLineItem2[] GetOrderlineItems()
{
return _orderLineItems.ToArray();
}
public void AddOrderLineItem(Product2 product, int quantity)
{
_orderLineItems.Add(new OrderLineItem2(product, quantity));
}
public decimal GetTotal()
{
return _orderLineItems.Sum(li => li.GetTotal());
}
}
public class OrderLineItem2
{
public OrderLineItem2(Product2 product, int quantity)
{
Product = product;
Quantity = quantity;
}
public Product2 Product { get; set; }
public int Quantity { get; set; }
public decimal GetTotal()
{
return Quantity*Product.Price;
}
}
public class Customer2
{
public string Name { get; set; }
}
public class Product2
{
public string Name { get; set; }
public decimal Price { get; set; }
}
□ View model
public class Order2Dto
{
public string CustomerName { get; set; }
public decimal Total { get; set; }
}
□ 映射配置
Mapper.CreateMap<Order2, Order2Dto>();
□ 使用
public ActionResult Complex()
{
var customer = new Customer2()
{
Name = "Darren"
};
var order = new Order2()
{
Customer = customer
};
var product = new Product2()
{
Name = "bosco",
Price = 5.00m
};
order.AddOrderLineItem(product, 10);
Order2Dto dto = Mapper.Map<Order2, Order2Dto>(order);
return View(dto);
}
□ 要点
目标中的属性遵循惯例:
○ 源中复杂属性名+复杂属性对应类的属性,构成了目标属性
○ 源中Get...()方法转换成目标中的...属性
AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器的更多相关文章
- AutoMapper在MVC中的运用06-一次性定义映射、复杂类型属性映射
本篇AutoMapper使用场景: ※ 当源和目标具有同名的复杂类型属性.集合类型属性,这2种属性对应的类间也需建立映射 ※ 一次性定义好源和目标的所有映射 ※ 一次性定义好源和目标的所有映射,目标中 ...
- AutoMapper在MVC中的运用小结
配置.单元测试.AOP注入 Decimal转换成String类型 源数组转换成目标数组 源中的集合(数组)属性转换成目标中的集合(数组)属性 子类父类间的映射 源字典集合转换成目标字典集合 枚举映射 ...
- AutoMapper在MVC中的运用01-配置、使用、单元测试、举例
MVC中,如果想在Domain Model和View Model之间建立映射,用AutoMapper是一个不错的选择.不仅如此,AutoMapper能在不同对象之间建立映射,比如string与int类 ...
- AutoMapper在MVC中的运用07-映射在订单场景的例子
本文参考了Taswar Bhatti的博客,他写了<Instant AutoMapper>这本书.遗憾的是,这本电子版书在国内还买不到,也下载不到.也只能从他的有限几篇博文中来窥探一二了. ...
- AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等
本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...
- AutoMapper在MVC中的运用05-映射中的忽略、处理null、多种映射转换
本篇AutoMapper使用场景: ※ 动态实现接口方法或属性 ※ 目标中的属性如果比源多,可以忽略多出的属性 ※ 目标有virtual属性,可忽略 ※ 目标属性值为null的解决办法 ※ int转s ...
- AutoMapper在MVC中的运用02-Decimal转String、集合、子父类映射
本篇AutoMapper使用场景: ※ Decimal转换成String类型 ※ 源数组转换成目标数组 ※ 源中的集合(数组)属性转换成目标中的集合(数组)属性 ※ 子类父类间的映射 Decimal转 ...
- MVC中System.InvalidOperationException: 传入字典的模型项的类型为“XXX”,但此字典需要类型“XXA”的模型项
出现此类错误的一个原因是Controller传过去的Model和View中的Model不是同一个Model
- mvc 中合并两个list集合
有时候,在进行查询操作的时候需要将从数据库中查询的两张表进行合并,成为一张表然后返回给前端.或者在原有的一张表基础上面加几个新的字段. 这个时候可以新建一个.class[model类],在这个新建的m ...
随机推荐
- 设置linux的console为串口【转】
转自:http://blog.chinaunix.net/uid-27717694-id-4074219.html 以Grub2为例:1. 修改文件/etc/default/grub #显示启动菜 ...
- APMServ5.2.6win10系统Apache、MySQL5.1启动失败解决办法
今天想在本地测试网站源码能否正常运行,如果可以就转空间了,然而下载了APMServ之后发现系统Apache.MySQL5.1启动均失败,小白的人表示只能借助百度,用了一个小时的时间终于解决了,虽然坎坷 ...
- High-Speed Tracking with Kernelized Correlation Filters
2015年的一篇论文,可参考:http://blog.csdn.net/carrierlxksuper/article/details/46461245. 另参考:http:// ...
- linux下实用命令
也使用了一段时间的debian,过程中总结了一些常用的又实用的命令,在这里分享给大家=.= 很多命令选项很多,在这里我只总结最实用的一些选项提供给大家,详细选项解释大家可以去问google. 1.df ...
- .NetCore 扩展封装 Expression<Func<T, bool>> 查询条件遇到的问题
前面的文章封装了查询条件 自己去组装条件,但是对 And Or 这种组合支持很差,但是也不是不能支持,只是要写更多的代码看起来很臃肿 根据 Where(Expression<Func< ...
- PHP 操作redis 封装的类 转的
<?php/** * Redis 操作,支持 Master/Slave 的负载集群 * * @author jackluo */class RedisCluster{ // ...
- CSS 浮动和清除
CSS 浮动和清除浮动 在写页面布局的过程中,浮动是大家经常用的属性.在好多的排版布局中都是用的的浮动比如说下面这些地方都是应用到了浮动. 在我学习浮动的时候可是熬坏了脑筋,在这里我分享一下我对浮动这 ...
- PHP 文件路径获取文件名
物理截取 $file = '/www/htdocs/inc/lib.inc.php'; $filename = basename($file); echo $filename, '<br/> ...
- linux设置最大打开文件数
一.查看当前用户对进程打开文件最大数的限制 $ ulimit -a | grep open 二.系统对进程打开文件最大数是如何限制的 先来看man的一段解析: /proc/sys/fs/file-ma ...
- Going Home
题意:n个人,进n个房子,每走一格花费1美元,每个房子只能进一人,求所有人进房子的最小花费. 就是推箱子 箱子最短行走距离 这题无法用bfs做 ! 用最小花费最大流 通过EK,Dinic,ISAP ...