The great automapper of the Nobody

Source: Internet
Author: User

Recently in the GXPT (University Platform project), the front frame is MVC, backstage with EF+WCF. How to say it suddenly feel very tall on the look. Oh! But when you write code, you find a problem. The Entity (DTO data Transfer object) required in front of the entity (Model) in EF is not the same, so you need to convert it. At first did not think much, may be the enthusiasm of the project to go to the head, write how much code do not feel the class, so every time you need to convert a write! Let's show you an example!

<span style= "FONT-SIZE:18PX;" >namespace testautomapper{    //Student Source entity classes public class    Studentsource    {public        string name {get; set;}        Public int. Age {get;set;}        public string Sex {get; set;}}    } </span>

<span style= "FONT-SIZE:18PX;" >namespace testautomapper{    //Student Objective entity class public class    Studentpurpost    {public        string name {get; set;} Public        string Age {get; set;}        public string Sex {get; set;}}    } </span>

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper{    class program    {        static void Main (string[] args)        {            //entity    to be converted This simulates the entity in EF            studentsource ssource = new Studentsource            {                name = "Zhang Hongjie", age                = all,                sex = "Male"            };< c12/>//simulates the entity that the foreground requires, the  converted entity!            studentpurpost spurpost = new Studentpurpost ();            Spurpost.age = SSource.age.ToString ();            Spurpost.name = Ssource.name;            Spurpost.sex = Ssource.sex;            Console.WriteLine (spurpost.name + spurpost.sex + spurpost.age);}}}    </span></span>

But this is the 1.1-point conversion, if the entity has dozens of attributes, oh, my God, I can't imagine. If it's a collection, don't write a loop. Really good trouble!

Later wrote write, feel bored, this looks too wasted time, there is no good way! The answer is clearly yes, the good way is really there, the answer is AutoMapper.

What is AutoMapper?

What is AutoMapper? Object-objectmapping tool, a tool for converting an object to another object! To make a metaphor, if in a program, the program is likened to a family, EF is dad, then AutoMapper is the mother, and DTO is the child! Mother is responsible for coordinating the relationship between father and son. Make money for the family and give it to Dad!

EntityFramework (father), AutoMapper (mother) and DTO (Child) you and I both know the official definition:

  • entityframework: Microsoft has developed an object-relational correspondence (O/R Mapping) solution based on ADO.
  • automapper:object-object mapping tool.
  • DTO: Data Transfer Object (data Transfer objects).

  EntityFramework has the "ORM" keyword in its definition, what is ORM?

ORM: Object Relational Mapping (Object/relation Mapping) is a result of the development of object-oriented software development methods. The object-oriented development method is the mainstream development method in the enterprise application development environment, and the relational database is the mainstream data storage system which is stored permanently in the enterprise-level application environment. object and relational data are two representations of business entities, and business entities behave as objects in memory and behave as relational data in the database. There are associations and inheritance relationships between objects in memory, and in a database, relational data cannot directly express many-to-many associations and inheritance relationships.  --Baidu encyclopedia

The concept is good to do, we will analyze, from the above definition can be seen: AutoMapper is the object-object mapping tool,EntityFramework is an ORM framework, DTO is a data transfer object (data Transferobject), note that all of the three definitions contain the Objects keyword, and there is no doubt that thework done by AutoMapper is the mapping transformation between "O" in the ORM and "O" in the DTO.  

So how do we apply it?

We'll refer to it before applying it. Click Library Package Manager under Tools to select the Package management Console! Then enter install-packageautomapper. then in the introduction of AutoMapper, you can use it safely!

Or the above example, the Studentsource entity is converted to a studentpurpost entity!

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper{    class program    {        static void Main (string[] args)        {            //create mapping Rule            Mapper.createmap<studentsource, studentpurpost> ();            Create an entity            studentsource ssource = new Studentsource            {                name = "Zhang Hongjie", age                = all,                sex = "Male"            };
   //to convert            var spurpost = mapper.map<studentpurpost> (ssource);            Console.WriteLine (spurpost.name + spurpost.sex + spurpost.age);        }    } </span>}</span>

So we can convert one entity to another! Now the situation is the same as the sub-attribute fields of two entities, so we can convert! Of course, such a simple collection of entities can also be converted!

<span Style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper{class Program {static void Main (string[] args) {//Create a transition relationship gauge            Then Mapper.createmap<studentsource, studentpurpost> ();            Create an entity collection list<studentsource> List = new list<studentsource> ();                Studentsource s = new Studentsource {name = "Hongjie", age = 13,            Sex = "Nan"}; List.            ADD (s); Convert list<studentpurpost> spurpostlist = Mapper.map<list<studentsource>, List<studentPurpos            T>> (list);        Console.WriteLine (spurpostlist[0].age + spurpostlist[0].name + spurpostlist[0].sex); }}}</span></span> 

How is it, feeling is not very cool. This saves us a lot of trouble, so that we can concentrate on our business logic, do not write this useless code wasted life!

This is the simple application of automapper, if still want to continue in-depth understanding of automapper, please continue to follow my blog, I will also launch other articles!


The great automapper of the Nobody

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.