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 classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; } }     Public classUserinfodto { Public stringName {Get;Set; }  Public stringAddress {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 classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; }  PublicTeacher Teacher {Get;Set; } }     Public classTeacher { Public intID {Get;Set; }  Public stringName {Get;Set; } }     Public classUserinfodto { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public string Teacher{Get;Set; } }    varuser =NewUserInfo {ID= A, name="Zhang San", Address="Beijing", teacher=NewTeacher {ID= One, name="Harry"                 }            }; varMapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto>(                NewDefaultmapconfig (). Convertusing<teacher,string> (t =t.name)); Userinfodto Userdto= Mapper. Map (user);

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

     Public classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; } }     Public classUserinfodto { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringuseraddress {Get;Set; } }    varMapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto>(                NewDefaultmapconfig (). Matchmembers ((x, y)=                {                    if(x = ="Address"&& y = ="useraddress")                    {                        return true; }                    returnx = =y;    })                ); Userinfodto Userdto= Mapper. Map (user);

4. Special handling of a field is required

     Public classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; } }     Public classUserinfodto { Public stringID {Get;Set; }  Public stringName {Get;Set; }  Public stringuseraddress {Get;Set; }  Public stringUserjson {Get;Set; } }    varuser =NewUserInfo {ID= A, name="Zhang San", Address="Beijing"            }; varMapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto>(                NewDefaultmapconfig (). PostProcess<UserInfoDTO> (value, state) = =                {                    //Add the year before the ID numberValue.id = DateTime.Now.ToString ("yyyy") +value.id; //JSON format for entitiesValue.userjson ="{\ "id\": \ ""+ Value.id +"\ ", \" name\ ": \""+ Value.name +"\"}"; returnvalue;     })            ); Userinfodto Userdto= Mapper. Map (user);

5. Ignore mappings for a field

     Public classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; } }     Public classUserinfodto { Public stringID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; } }    varuser =NewUserInfo {ID= A, name="Zhang San", Address="Beijing"            }; varMapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto>(                NewDefaultmapconfig (). Ignoremembers<userinfo, Userinfodto> (New string[] {"name" })            ); Userinfodto Userdto= Mapper. Map (user);

6. Assigning a default value to an empty element

     Public classUserInfo { Public intID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; }  PublicDatetime? godate {Get;Set; } }     Public classUserinfodto { Public stringID {Get;Set; }  Public stringName {Get;Set; }  Public stringAddress {Get;Set; }  PublicDateTime Godate {Get;Set; } }    varuser =NewUserInfo {ID= A, name="Zhang San", Address=NULL, Godate=NULL            }; varMapper = Objectmappermanager.defaultinstance.getmapper<userinfo, userinfodto>(                NewDefaultmapconfig ()//If the date is empty set to the current time. Nullsubstitution<datetime?, datetime> (value) =DateTime.Now)//if the string type is NULL, assign the value "". nullsubstitution<string,string> (value) ="")            ); Userinfodto Userdto= Mapper. Map (user);

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

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.