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