AutoMapper在MVC中的运用03
来源:互联网 发布:crossover mac 序列号 编辑:程序博客网 时间:2024/06/05 22:29
本篇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中的运用01
- AutoMapper在MVC中的运用02
- 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用户登陆中的运用
- JAVA.SWT/JFace: SWT基本组件之按钮(Button)
- 基于OpenCV的程序脱离动态链接库运行方法
- 基于SIFT特征的全景图像拼接
- eclipse 安装插件 Aptana
- 区分全半角字符
- AutoMapper在MVC中的运用03
- 消除SDK更新时的“https://dl-ssl.google.com refused”错误 .
- ajax跨域post请求的java代理实现
- VC++工作第一天Dialog对话框加载静态背景图片之一BITMAP。
- RHEL6.4下安装qt-recordMyDesktop屏幕录像软件
- Android volley 应用 GET POST请求 图片异步加载
- 七大经典排序【 交换排序】之冒泡排序
- 辞旧迎新 be better---记我的2013
- Python源码剖析----第三章(下)