A Preliminary Study on AutoMapper and AutoMapper

Source: Internet
Author: User

A Preliminary Study on AutoMapper and AutoMapper
Preface

I know AutoMapper for a long time, but it has never been used. Recently, a scenario needs to be used, which is really a powerful tool. After reading the wiki on git, I found that the content on the wiki is actually comprehensive, and I haven't found it yet. However, I had a chat with my friends in the group and thought it was quite good to act as a porter for a small translation, but the younger brother was not talented. I hope the audience could pat the bricks.

What is AutoMapper?

AutoMapper is a perfect Mapping tool. Mapping refers to data object conversion. If you borrow friends from a group, it is mostly used for Mapping of domain objects and DTO objects or Mapping of SqlDataReader.

Git home: https://github.com/AutoMapper/AutoMapper

Wiki home: https://github.com/AutoMapper/AutoMapper/wiki

Simple Example

In this scenario, there are three objects: Order, Customer, and Product in the Order business. The Product is included in the OrderLinItem object:

1 public class Order 2 {3 private readonly IList <OrderLineItem> _ orderLineItems = new List <OrderLineItem> (); 4 5 public Customer {get; set ;} 6 7 public OrderLineItem [] GetOrderLineItems () 8 {9 return _ orderLineItems. toArray (); 10} 11 12 public void AddOrderLineItem (Product product, int quantity) 13 {14 _ orderLineItems. add (new OrderLineItem (product, quantity); 15} 16 17 public decimal GetTotal () 18 {19 return _ orderLineItems. sum (li => li. getTotal (); 20} 21} 22} 23 24 public class OrderLineItem25 {26 public OrderLineItem (Product product, int quantity) 27 {28 Product = product; 29 Quantity = quantity; 30} 31 32 public Product {get; private set;} 33 public int Quantity {get; private set;} 34 35 public decimal GetTotal () 36 {37 return Quantity * Product. price; 38} 39} 40 41 public class Customer42 {43 public string Name {get; set;} 44} 45 46 public class Product47 {48 public decimal Price {get; set ;} 49 public string Name {get; set;} 50}View Code

Then I need to display some data on the page. To keep the page ViewModel independent, define a DTO (OrderDto) that only contains the fields required by the page, as shown below:

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

Then the problem arises. How can this problem be converted?

I think the simplest method is:

// Define an ordervar customer = new Customer {Name = "George Costanza";} var order = new Order {Customer = customer ;} var bosco = new Product {Name = "Bosco", Price = 4.99 m;} order. addOrderLineItem (bosco, 15); // convert var dto = new OrderDto (); dto. customerName = order. customer. name; dto. total = order. getTotal ();

The code above seems simple and no problem, but what if there are many dto attributes? Will repeated manual Mapping be a little tired? At this time, the siege lion has to play the spirit of laziness, the following is the use of the AutoMapper code:

// Configure an AutoMapper ing Mapper. CreateMap <Order, OrderDto> (); // execute mappingOrderDto dto = Mapper. Map <Order, OrderDto> (order );

Is it simple and concise, but we will find that this Mapping is a little complicated. CustomerName corresponds to order. Customer. Name; Total corresponds to the GetTotal method. How does AutoMaper define this rule?

Simple Mapping rules for AutoMapper

1. for ing between complex objects and simple objects, Mapping follows the camper naming rules, that is, when the target dto attribute name cannot be found in the source object, autoMapper splits the dto attribute names (for example, CustomerName => Customer. name) to find the corresponding relationship.

2. For attributes starting with Get (such as Total => GetTotal) in the source object, if the attribute with the same name cannot be found, the Get + PropertyName method will be matched.

 

Flattening from AutoMapper Wiki: https://github.com/AutoMapper/AutoMapper/wiki/Flattening

The above rules are the simplest matching rules. For Complex Mapping, which completely does not comply with the above two features, you need to customize the Mapping configuration, in the next section "Custom er ing", I will handle it for you. If you have any questions or comments, please leave a message for me. Let's discuss it. Please kindly advise me ~!


What is the role of automapper in ORM?

AutoMapper? This is the simplest example.
For example, in ORM, the Model used for interacting with databases has many attribute variable methods. When we interact with other systems (or other structures in the system), for coupling or security or performance considerations (in short, various considerations ), we do not want to pass this Model directly to them. In this case, we will create an anemia Model to store and transmit data. Is Shenma an anemia model? The anemia model (DTO, Data Transfer Object) contains only the attributes of Shenma, and can only save the required Data. Wood has any other redundant method Data or something, A type object used for data transmission. During the creation process, if we do this manually, we will see the Code as follows:
B B = new B ();
B. XXX1 = a. XXX1;
B. XXX2 = a. XXX2;
...
...
...
Return B;

In this case, AutoMapper can automatically map A model to A new B model based on the definitions in model A and model B. This prevents code from being smelly, long, and boring. SO, can be understood as a code generator ......

Who can help me translate ??

Window
Tile tiles all Windows, so you can see all of them
In the cascade, all Windows instances are added to each other.
Schedule Minimum Windows at the bottom of the screen
Freeze the split output window to view the scrollback
Refresh and repaint the current window

Clearly erase the screen (but keep scrollback)
Empty clear scrollback buffer and reclaim memory
Command Buffer open full window command Editor
History display commands
Status display status window
Automapper display automapper window
Help
Content display directory
Search online help Database
Reference display command reference
Entry help

Command wizard for help and example zmud command
Function Wizard for help and examples of zmud Functions
Start the initial welcome dialog box displayed on the screen
Send feedback to the author's zmud
Register and enter your registration code as the firmware version.
Version, credit, and copyright information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.