AutoMapper (5), automapper

Source: Internet
Author: User

AutoMapper (5), automapper
Total returned directory

Dynamic and ExpandoObject ing

AutoMapper can map or map from a dynamic (dynamic) object to a dynamic object without any configuration.

Namespace extends thautomapper {// defines a Person class public class Person {public int Age {get; set;} public string Name {get; set ;}} // Main Program class Program {static void Main (string [] args) {// ing is also possible without CreateMap, which is called "Zero Configuration" // Mapper. createMap <MyDynamicClass, Person> (). reverseMap (); // map a dynamic object to a common instance dynamic dynamicObj = new ExpandoObject (); // The ExpandoObject object contains a member dynamicObj that can be dynamically added or removed at runtime. age = 103; dynamicObj. name = "tkb to simplified"; Person person = Mapper. map <Person> (dynamicObj); Console. writeLine ("person. age = {0}, Name = {1} ", person. age, person. name); // map a common instance to a dynamic object dynamic dynamicObj2 = Mapper. map <ExpandoObject> (person); Console. writeLine ("dynamicObj2.Age = {0}, Name = {1}", dynamicObj2.Age, dynamicObj2.Name); Console. read ();}}}

The description of this program is clearly commented out in the code, and no further explanation is needed.

Flat

The common usage of ing between objects is to flatten a complex model into a simpler model. For demonstration purposes, I will define several classes here. The Code is as follows:

namespace FifthAutoMapper{    public class Order    {        public Customer Customer { get; set; }        public decimal GetTotal()        {            return 100M;        }    }    public class Customer    {        public string Name { get; set; }    }    public class OrderDto    {        public string CustomerName { get; set; }        public decimal Total { get; set; }    }}

Order class: it is a common Order class. Of course, the actual project will certainly have many attributes. Here, only one attribute and one method are reserved for convenience demonstration.

Customer class: Customer class, which defines the Customer's name.

OrderDto class: the class after Order flattening, which contains data of specific requirements.

Official definition:When the CreateMap method is used to configure the source type and target type in AutoMapper, The configurator tries to match the source attribute and method to the target attribute. If any attribute of the Target attribute does not exist in the source type attribute, method, or method prefixed with Get, AutoMapper will name the target Member (according to PascalCase practices) separate words.

The following code is added to the Main method:

Mapper. createMap <Order, OrderDto> (); var order = new Order () {Customer = new Customer () {Name = "tkb to simplified" },}; var orderDto = Mapper. map <OrderDto> (order); Console. writeLine (orderDto. customerName); Console. writeLine (orderDto. total );

Although the previous blog has already said a lot, I am still explaining it here.

We configured the type ing in the Createmap method of AutoMapper. AutoMapper can only map the type pairs It knows. Therefore, we use CreateMap to register the source/target type pairs explicitly. To execute the ing, we use the Map method.

In the OrderDto class, the Total attribute matches the GetTotal method on the Order. The CustomerName attribute matches the Customer. Name attribute on the Order. In short, as long as the target type attribute is properly named, we do not need to configure individual attribute matching.

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.