Summary of Use of Emitmapper

Source: Internet
Author: User

Recently, a JS framework was used at the front end of the company development project, and the backend was generated using the entity's DTO with the JSON read by the Ef,js foreground.

See on the net Emitmapper relative to other mapping framework processing speed can be faster, it is used. Here are a few blocks commonly used in the code.

1. Normal mapping.

    public class UserInfo    {public        int id {get; set;}        public string name {get; set;}        public string Address {get; set;}    }    public class Userinfodto    {public                string name {get; set;}        public string Address {get; set;}    }    var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> ();
Userinfodto userdto = mapper. Map (user);

2. There is a foreign key association, you need to map the name of the outside key

 public class UserInfo {public int id {get; set;}        public string name {get; set;}        public string Address {get; set;}    Public Teacher Teacher {get; set;}        public class Teacher {public int id {get; set;}    public string name {get; set;}        public class Userinfodto {public int id {get; set;}        public string name {get; set;}    public string Teacher {get; set;}                } var user = new UserInfo {id = a, name = "Zhang San", address = "Beijing",            Teacher = new Teacher {id = one, name = "Harry"}     }; var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> (New defaultmapconf IG ().     Convertusing<teacher, string> (t = t.name)); Userinfodto userdto = mapper. Map (user); 

3. Two entities have inconsistent names and need to be mapped.

    public class UserInfo    {public        int id {get; set;}        public string name {get; set;}        public string Address {get; set;}    }    public class Userinfodto    {public        int id {get; set;}        public string name {get; set;}        public string UserAddress {get; set;}    }    var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> (                new Defaultmapconfig ( )                . Matchmembers ((x, y) = =                {                    if (x = = "Address" && y = = "useraddress")                    {                        return true;                    }                    return x = = y;                })                ;    Userinfodto userdto = mapper. Map (user);

4. Special handling of a field is required

    public class UserInfo {public int id {get; set;}        public string name {get; set;}    public string Address {get; set;}        public class Userinfodto {public string id {get; set;}        public string name {get; set;}        public string UserAddress {get; set;}    public string Userjson {get; set;}            } var user = new UserInfo {id = a, name = "Zhang San", address = "Beijing"    }; var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> (New defaultmapconf IG ().                    Postprocess<userinfodto> (value, state) = + {//before ID number plus year                    Value.id = DateTime.Now.ToString ("yyyy") + value.id;                    The JSON format of the entity Value.userjson = "{\" id\ ": \" "+ value.id +" \ ", \" name\ ": \" "+ Value.name +" \ "}";                return value;       })     ); Userinfodto userdto = mapper. Map (user);

5. Ignore mappings for a field

    public class UserInfo    {public        int id {get; set;}        public string name {get; set;}        public string Address {get; set;}    }    public class Userinfodto    {public        string ID {get; set;}        public string name {get; set;}        public string Address {get; set;}    }    var user = new UserInfo {                 id = n,                 name = "Zhang San",                 address = "Beijing"            };    var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> (                new Defaultmapconfig ( )                . Ignoremembers<userinfo, userinfodto> (new string[] {"Name"})            ;    Userinfodto userdto = mapper. Map (user);

6. Assigning a default value to an empty element

 public class UserInfo {public int id {get; set;}        public string name {get; set;}        public string Address {get; set;} Public DateTime?    godate {get; set;}        public class Userinfodto {public string id {get; set;}        public string name {get; set;}        public string Address {get; set;}    Public DateTime godate {get; set;}                } var user = new UserInfo {id = a, name = "Zhang San", address = null,    Godate = null}; var mapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto> (New defaultmapconf IG ()//If the date is empty set to the current time.                Nullsubstitution<datetime?, datetime> ((value) = DateTime.Now)//If the string type is null-assigned to "" .    Nullsubstitution<string, String> ((value) = "")); Userinfodto userdto = mapper. Map (user); 

Commonly used on the above points, for more in-depth. Pending study .....

Summary of Use of Emitmapper

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.