博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
平坦化
阅读量:7112 次
发布时间:2019-06-28

本文共 3129 字,大约阅读时间需要 10 分钟。

Flattening

One of the common usages of object-object mapping is to take a complex object model and flatten it to a simpler model. You can take a complex model such as:

public class Order    {        private readonly IList
_orderLineItems = new List
(); public Customer Customer { get; set; } public OrderLineItem[] GetOrderLineItems() { return _orderLineItems.ToArray(); } public void AddOrderLineItem(Product product, int quantity) { _orderLineItems.Add(new OrderLineItem(product, quantity)); } public decimal GetTotal() { return _orderLineItems.Sum(li => li.GetTotal()); } } public class Product { public decimal Price { get; set; } public string Name { get; set; } } public class OrderLineItem { public OrderLineItem(Product product, int quantity) { Product = product; Quantity = quantity; } public Product Product { get; private set; } public int Quantity { get; private set;} public decimal GetTotal() { return Quantity*Product.Price; } } public class Customer { public string Name { get; set; } }

We want to flatten this complex Order object into a simpler OrderDto that contains only the data needed for a certain scenario:

public class OrderDto    {        public string CustomerName { get; set; }        public decimal Total { get; set; }    }

When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).

// Complex model        var customer = new Customer        {            Name = "George Costanza"        };    var order = new Order        {            Customer = customer        };    var bosco = new Product        {            Name = "Bosco",            Price = 4.99m        };    order.AddOrderLineItem(bosco, 15);        // Configure AutoMapper        Mapper.Initialize(cfg => cfg.CreateMap
()); // Perform mapping OrderDto dto = Mapper.Map
(order); dto.CustomerName.ShouldEqual("George Costanza"); dto.Total.ShouldEqual(74.85m);

We configured the type map in AutoMapper with the CreateMap method. AutoMapper can only map type pairs it knows about, so we have explicitly register the source/destination type pair with CreateMap. To perform the mapping, we use the Map method.

On the OrderDto type, the Total property matched to the GetTotal() method on Order. The CustomerName property matched to the Customer.Name property on Order. As long as we name our destination properties appropriately, we do not need to configure individual property matching.

转载于:https://www.cnblogs.com/Leman/p/5774347.html

你可能感兴趣的文章
LeetCode 406 Queue Reconstruction by Height
查看>>
四种遍历方法你选哪个?
查看>>
LeetCode41.缺失的第一个正数 JavaScript
查看>>
Java设计模式五——单件模式
查看>>
CI第一篇 Jenkins+github fir im 蒲公英pgyer com
查看>>
webpack 搭建 vue 项目
查看>>
当TensorFlow遇上Kubernetes ---中兴通讯人工智能计算平台的技术实践
查看>>
奇怪的 Ruby
查看>>
PAT A1084
查看>>
79. Word Search
查看>>
【Android】RxJava的使用(四)线程控制 —— Scheduler
查看>>
极限编程 (Extreme Programming) - 迭代计划 (Iterative Planning)
查看>>
小程序外卖购物车 直接就能用~
查看>>
Python版设计模式之监听者模式
查看>>
[Spring Security 5.2.0 翻译] 8 Architecture and Implementation
查看>>
使用 Sphinx 撰写技术文档并生成 PDF 总结
查看>>
Fastjson的基本使用方法大全
查看>>
SSH 超时设置
查看>>
React-setState杂记
查看>>
Dojo 如何测试 widget
查看>>