打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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...()方法转换成目标中的...属性


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
让AutoMapper在你的项目里飞一会儿(转)
【AutoMapper官方文档】DTO与Domin Model相互转换(中)
DDD领域驱动设计初探(5):AutoMapper使用
AutoMapper简明教程(学习笔记)
彻底理解ThreadLocal
threadlocal
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服