AutoMapper在MVC中的运用02
来源:互联网 发布:豫湘桂 知乎 编辑:程序博客网 时间:2024/06/06 02:50
本篇AutoMapper使用场景:
※ Decimal转换成String类型
※ 源数组转换成目标数组
※ 源中的集合(数组)属性转换成目标中的集合(数组)属性
※ 子类父类间的映射
Decimal转换成String类型
□ Domain model
public class Order
{
public decimal Amount { get; set; }
}
□ View model
public class OrderListViewModel
{
public string Amount { get; set; }
}
□ 通过实现ValueFormatter<>,可以实现类型的转换
public class MoneyFormatter : ValueFormatter<decimal>
{
protected override string FormatValueCore(decimal value)
{
return value.ToString("C");
}
}
□ 映射配置
AddFormatter<MoneyFormatter>();
Mapper.CreateMap<Order, OrderListViewModel>();
□ 使用
public ActionResult Index()
{
var order = new Order {Amount = 85M};
var orderListViewModel = Mapper.Map<Order, OrderListViewModel>(order);
return View(orderListViewModel);
}
□ 要点
属性类型的转换,我们可以自定义ValueFormatter<>。
源数组转换成目标数组
□ Domain model
public class Source
{
public int Value { get; set; }
}
□ View model
public class Dest
{
public int Value { get; set; }
}
□ 映射配置
Mapper.CreateMap<Source, Dest>();
□ 使用
public ActionResult ArraySample()
{
var sourceArray = new[]
{
new Source{Value = 1},
new Source{Value = 5},
new Source{Value = 10}
};
var destArray = Mapper.Map<Source[], Dest[]>(sourceArray);
return View(destArray.ToList());
}
□ 要点
创建映射是单个类之间的映射,但可以根据源集合获取到目标集合。
源中的集合(数组)属性转换成目标中的集合(数组)属性
□ Domain model
public class Order1
{
private IList<Order1Line> _lineItems = new List<Order1Line>();
public Order1Line[] LineItems
{
get { return _lineItems.ToArray(); }
}
public void AddLineItem(Order1Line orderLine)
{
_lineItems.Add(orderLine);
}
}
public class Order1Line
{
public int Quantity { get; set; }
}
□ View model
public class Order1Dto
{
public Order1LineDto[] LineItems { get; set; }
}
□ 映射配置
Mapper.CreateMap<Order1, Order1Dto>();
□ 使用
public ActionResult ArraySample1()
{
var order1 = new Order1();
order1.AddLineItem(new Order1Line(){Quantity =5});
order1.AddLineItem(new Order1Line(){Quantity = 15});
order1.AddLineItem(new Order1Line(){Quantity = 25});
var order1Dto = Mapper.Map<Order1, Order1Dto>(order1);
return View(order1Dto);
}
子类父类间的映射
//父类
public class Foo
{
public int Value { get; set; }
}
//子类
public class Bar : Foo
{
pubic int Value{get;set;}
}
public ActionResult JiCheng()
{
var source = new Bar() {Value = 5};
var dest = Mapper.Map<Bar, Foo>(source);
return View(dest);
}
□ 要点
继承关系类之间的映射不需要做映射配置
- AutoMapper在MVC中的运用02
- AutoMapper在MVC中的运用01
- AutoMapper在MVC中的运用03
- AutoMapper在MVC中的运用04
- AutoMapper在MVC中的运用05
- AutoMapper在MVC中的运用06
- AutoMapper在MVC中的运用07
- AutoMapper在MVC中的运用小结
- Json Ajax 在Mvc中的简单运用
- MVC模式在android 中的运用
- MVC架构模式在Tkinter中的运用
- Nop- Automapper在Nopcommerce中的使用<五>
- Nop- Automapper在Nopcommerce中的使用<五>
- AutoMapper在的项目中的使用
- AutoMapper在asp.netcore中的使用
- AutoMapper运用实例
- PHP中的MVC运用
- MVC设计模式在asp用户登陆中的运用
- 2012年蓝桥杯初赛题1
- 关于Coco2dx中锚点的问题,游戏坐标系,节点坐标系
- QT应用之透明背景
- POJ 1661 Help Jimmy -- 动态规划
- ubuntu 查看jdk位置
- AutoMapper在MVC中的运用02
- 趁年华未老
- Android消息循环
- 影响空间规模的几种数据存储结构
- xcode自动检测内存泄露
- js控制控件显示实例dhtmlxcombo
- 全排列问题
- ehcache config xml file
- hadoop使用lzo压缩文件笔记 (cdh3u0)