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 .....