AutoMapper Introduction (not finished, partially not implemented)

Source: Internet
Author: User

The Inter-Entity conversion tool. In fact, you can also use JSON to achieve the same name property, the name of the property (indicated by Jsonproperty) automatic conversion

Latest Version 6.11

Need to use vs2013 above. vs2012 Download the new version of NuGet will encounter problems, only the older version.

1 usingAutoMapper;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingsystem.web;6 usingSYSTEM.WEB.MVC;7 8 namespaceautomappervs2015.controllers9 {Ten      Public classHomecontroller:controller One     { A          Publicactionresult Index () -         { -             returnView (); the         } -  -          PublicActionResult About () -         { +Viewbag.message ="Your Application description page."; -  +             returnView (); A         } at  -          Publicactionresult Contact () -         { -Viewbag.message ="Your Contact page."; -  -             returnView (); in         } -  to          Publicjsonresult testautomaper () +         { -             //Table=>dto. Attributes are automatically matched, and the component supports automatic matching of property names for named Specifications theMapper.initialize (cfg =//https://github.com/AutoMapper/AutoMapper/wiki/Getting-started *             { $Cfg. Createmap<tb_menu, dto_menu>();Panax Notoginseng                 //CFG. Createmap<dto_menu, tb_menu> (); -             }); the             varTbmenu =NewTb_menu () {menu_name ="Rights Management", Menu_level ="1" }; +             varDTO = mapper.map<dto_menu>(tbmenu); A             returnJson (DTO, jsonrequestbehavior.allowget); the  +             //dto=>table. Attributes are automatically matched, and the component supports automatic matching of property names for named Specifications -             //mapper.initialize (cfg = { $             //    //CFG. Createmap<tb_menu, dto_menu> (); $             //CFG. Createmap<dto_menu, tb_menu> (); -             //}); -             //var dtomenu = new Dto_menu () {menu_name = "Rights Management 2", Menu_level = "2"}; the             //var tbmenu = mapper.map<tb_menu> (dtomenu); -             //return Json (Tbmenu, jsonrequestbehavior.allowget);Wuyi  the  -             //Table=>dto. Attributes with the same auto-match, which are not identical and do not conform to the naming specification, need to define the corresponding matching relationship manually Wu             //TODO: The manual definition method is unknown, the document is not found, the old document is invalid (http://www.cnblogs.com/xishuai/p/3700052.html).  -             //mapper.initialize (cfg = About             //{ $             //CFG. Createmap<tb_menu, Dto_menu> (). Formember (), -             //}); -             //var dtomenu = new Dto_menu () {menu_name = "Rights Management 2", Menu_level = "2"}; -             //var tbmenu = mapper.map<tb_menu> (dtomenu); A             //return Json (Tbmenu, jsonrequestbehavior.allowget); +  the         } -     } $  the      Public classTb_menu the     { the          PublicTb_menu () the         { -         } in  the          Public stringMenu_level {Get;Set; } the          Public stringMenu_name {Get;Set; } About     } the  the      Public classDto_menu the     { +          PublicDto_menu () -         { the         }Bayi  the          Public stringMenu_level {Get;Set; } the          Public stringMenu_name {Get;Set; } -     } -}

Continued://todo: The manual definition method is unknown, the document is not found, the old document is invalidated (http://www.cnblogs.com/xishuai/p/3700052.html).

New Document: https://github.com/AutoMapper/AutoMapper/wiki/Getting-started (official website)

Old document reference (many methods are already incorrect, deprecated, vs compilation failed in the new version)

Http://www.cnblogs.com/xishuai/p/3700052.html

Argument: The property has the same auto-match, and the component supports the naming specification's property name auto-match. This can also be implemented in JSON, which is mentioned at the beginning of this article.

You can see that the configuration is quite simple, as long as you set the type mapping between order and orderdto, and we see that the CustomerName and total properties in Orderdto are not relative to the domain model order, nothing can be converted, Feel good magic appearance, in fact, carefully found that the naming of these properties have a certain rule, automapper in doing parsing will follow the Pascalcase (Pascal named), is a variable name method, In addition to the Pascalcase and the Hungarian (Hungarian nomenclature) and CamelCase (Camel nomenclature), pascalcase refers to the combination of uppercase and lowercase letters to form the names of variables and functions, the first letter capitalized, the CamelCase the first letter lowercase, our C # Naming, the general use of CamelCase and Pascalcase, the more advanced is pascalcase.

But why would automapper parse total? Because there is a gettotal () method in the domain model order, AutoMapper will parse the word after "get", so it will correspond to total, and if you change the Orderdto attribute "all" to "Totals", you will find the " Totals "is 0. Understanding the analytic way of AutoMapper, we should pay attention to the writing of variables, attributes or method names must be standardized, this is a good habit.

http://blog.csdn.net/csethcrm/article/details/52934325

AutoMapper Introduction (not finished, partially not implemented)

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.