One common use of object-object mapping is to get a complex object model and expand it into a simpler model. You can take a complex model, such as:
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 classProduct - { - Public decimalPrice {Get;Set; } - Public stringName {Get;Set; } - } - in Public classOrderlineitem - { to PublicOrderlineitem (product product,intquantity) + { -Product =product; theQuantity =quantity; * } $ Panax Notoginseng PublicProduct Product {Get;Private Set; } - Public intQuantity {Get;Private Set;} the + Public decimalgettotal () A { the returnquantity*Product.price; + } - } $ $ Public classCustomer - { - Public stringName {Get;Set; } the}
We want to turn this complex order object into a simpler orderdto that contains only the data needed for a scenario:
1 Public class Orderdto 2 {3public stringgetset;} 4 Public decimal Get Set ; } 5 }
When you configure a source/target type pair in AutoMapper, the configuration program attempts to match the properties and methods on the source type with the properties on the target type. If there is no source type on any of the properties on the target type, the method, or the method prefixed with "Get", then AutoMapper splits the target member name into a single word (following the Pascal orthography Convention).
1 //Complex Model2 3 varCustomer =NewCustomer4 {5Name ="George Costanza"6 };7 varOrder =NewOrder8 {9Customer =CustomerTen }; One varBosco =NewProduct A { -Name ="Bosco", -Price =4.99m the }; -Order. Addorderlineitem (Bosco, the); - - //Configure AutoMapper + -Mapper.initialize (cfg = cfg. Createmap<order, orderdto>()); + A //Perform Mapping at -Orderdto dto = Mapper.map<order, orderdto>(order); - -Dto. Customername.shouldequal ("George Costanza"); -Dto. Total.shouldequal (74.85m);
We use the Createmap method to configure type mappings in AutoMapper. AutoMapper can only map the type pairs that it knows, so we must explicitly register the source/target type pair using Createmap. To perform the mapping, we use the map method.
On the Orderdto type, the total property matches the Gettotal () method on order. The CustomerName property matches the Customer.name property on the order. As long as we properly name the target attribute, we do not need to configure a single property match.
0.AutoMapper
1. Flattening
2. Projection
3. Configuration Verification
4. Lists and arrays
5. Nested mappings
6. Custom type Converters
7. Custom Value Resolver
8. Empty replacement
9. Before and after mapping operations
10. Dependency Injection
11. Mapping Inheritance
12. queryable Extensions (LINQ)
13. Configuration
14. Conditional mapping
15. Open Generics
16. Understand your mapping
1. Flattening