Write in front
Know AutoMapper for a long time, but has been no use, recently just have a scene need to use, indeed is a sharp weapon. Read the wiki on git, found that the content is actually written on the wiki is very comprehensive, in-depth for the moment has not been excavated. But with the group of friends to communicate under, feel as the next porter, small translation is very good, little brother, I hope that the spectators Daniel Pat bricks.
What is AutoMapper?
AutoMapper is a perfect mapping tool, the so-called mapping is the conversion of data objects, borrowed from the group of Friends is more used for domain objects and DTO objects mapping or SqlDataReader mapping.
Git home: Https://github.com/AutoMapper/AutoMapper
Wiki home: Https://github.com/AutoMapper/AutoMapper/wiki
A simple example
Here's the scene. In order business, there are order, Customer, product three objects as follows, where product is included in the Orderlinitem object:
1 Public classOrder2 {3 Private ReadOnlyIlist<orderlineitem> _orderlineitems =NewList<orderlineitem>();4 5 PublicCustomer Customer {Get;Set; }6 7 Publicorderlineitem[] Getorderlineitems ()8 {9 return_orderlineitems.toarray ();Ten } One A Public voidAddorderlineitem (product product,intquantity) - { -_orderlineitems.add (NewOrderlineitem (product, quantity)); the } - - Public decimalgettotal () - { + return_orderlineitems.sum (Li =Li. Gettotal ()); - } + } A } at - Public classOrderlineitem - { - PublicOrderlineitem (product product,intquantity) - { -Product =product; inQuantity =quantity; - } to + PublicProduct Product {Get;Private Set; } - Public intQuantity {Get;Private Set;} the * Public decimalgettotal () $ {Panax Notoginseng returnquantity*Product.price; - } the } + A Public classCustomer the { + Public stringName {Get;Set; } - } $ $ Public classProduct - { - Public decimalPrice {Get;Set; } the Public stringName {Get;Set; } -}View Code
Then on the page I need to show some data, in order to keep the page ViewModel independent, defining a DTO (Orderdto) contains only the fields required by the page, as follows:
Public class orderdto{ publicstringgetset;} Public decimal Get Set ; }}
View Code
Then the question came, how to convert it?
I think the simplest way is definitely this:
//Define an ordervarCustomer =Newcustomer{Name="George Costanza";}varOrder =Neworder{Customer=customer;}varBosco =Newproduct{Name="Bosco", Price=4.99m;} Order. Addorderlineitem (Bosco, the);//ConversionvarDTO =Neworderdto ();d to. CustomerName=order. Customer.name;dto. Total= order. Gettotal ();
The code above looks simple enough, but what if there are a lot of dto attributes? Repeated manual mapping will be a bit tired? At this time the siege Lions have to play lazy spirit, the following is the use of AutoMapper code:
// Configuring a AutoMapper map mapper.createmap<order, orderdto>(); // Executive Mapping Orderdto dto = Mapper.map<order, orderdto> (Order);
is not very simple concise, but careful will find that this mapping a little bit more complex, customername corresponds to Order.Customer.Name; Total corresponds to the Gettotal method. How does automaper do it and how does it define the rule?
Simple default mapping rule for AutoMapper
1. For the mapping of complex objects to simple objects, mapping follows the rule of hump naming, that is, when the target DTO property name is not found in the source object, AutoMapper splits the DTO attribute name according to the Hump naming rule (for example:customername=> Customer.name) to find a corresponding relationship.
2. For a property that has a get start in the source object (for example, total = Gettotal), the Get+propertyname method is matched if no property of the same name is found.
This article is handled from AutoMapper wiki flattening:https://github.com/automapper/automapper/wiki/flattening
The above rule is the simplest matching rule, for complex mapping, completely does not conform to the above two characteristics, then need to do mapping custom configuration, the next section of the "AutoMapper custom mapping", I will carry for everyone. Have any questions and comments can give me a message, we come to discuss some, please danale generous enlighten ~!
A tentative study of AutoMapper Porter AutoMapper