AutoMapper (i)--implementation of conversions between data contracts and entity classes

Source: Internet
Author: User

Previously did the object to object conversion method of encapsulation, the underlying is mainly by reflection value assignment + loop to achieve. Recently in the company to maintain the old system, the system used WCF, then very curious about their data contract to the transformation of the entity model what to do, then look at the source code, found that they actually is a property of a property of the assignment came over, then gave me a ray, there is no one to lift this place can be encapsulated it?

As far as I know, there are three ways to implement this entity class to the entity class conversion process, below to briefly describe the following three ways.

The first, but also the simplest, is to use the list to bring the conversion method, but this method needs to define the conversion rules, writing it is also very troublesome, so at that time only to do a small demo for everyone to see, also did not promote the use;

The second is to use the Reflection + generic encapsulation method, but this method can only realize the transformation of simple attributes, for the processing of complex objects will appear the problem of attribute assignment;

The third approach is to use the Microsoft packaged automapper.

Next, this paper mainly introduces the use of AutoMapper.

First, the introduction

First find the Package Manager console:

then enter:

After use, there are also the following using:

Using AutoMapper;  Use AutoMapper to introduce this namespace

Next we'll write two classes and write out how the two classes are translated specifically:

SOURCE class:

Public  class Student    {public       int id;//id public       string name;//name public       string sex;//sex    }

Target class;

  public class Studentgoal    {public        int id;//id public        string name;//name public        string sex;//gender    }

Next look at the transitions between individual entities and the conversions between lists:

   #region Conversion between individual entities demo//create mapping rules Mapper.createmap<student, studentgoal> ();                    Create a source entity Student Stuscource = new Student {ID = 1,                Name = "LHC", Sex = "Girl"};                Conversion: Convert the source target to the destination var sgoal = mapper.map<studentgoal> (Stuscource);            Output converted entity Name Console.WriteLine (sgoal.name); #endregion Conversion between #region List demo//Create conversion rule mapper.createmap<student                    , studentgoal> ();                        Create source Entity collection List<student> Listscource = new List<student> () {                    New Student () {id=1,name = "Liu Huishuo", sex = "female"}, new Student () {id=2,name = "small 77", sex = "female"}                    }; To convert list<studentgoal> LisTgoal = Mapper.map<list<student>, list<studentgoal>> (Listscource); Output target collection student name foreach (Var stu in Listgoal) {Console.writel Ine (Stu.                    Name); } #endregion

Hey, feel very convenient bar ~ ~

This is just a simple use, automapper in the entity conversion is still quite mature, we use, can be further encapsulated on this basis.

AutoMapper (i)--implementation of conversions between data contracts and entity classes

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.