EmitMapper的使用小结
最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成。
在网上看到了EmitMapper相对其他映射框架处理速度可以更快,就拿来用了。下面是代码中常用的几个块。
1.普通的映射。
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.有外键关联,需要映射出外键所带名字
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 = 12, name = "张三", address = "北京", teacher = new Teacher { id = 11, name = "王五" } }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .ConvertUsing<Teacher, string>(t => t.name) ); UserInfoDTO userdto = mapper.Map(user);
3.两个实体之间名字不一致,需要映射。
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.需要对某一个字段进行特殊处理
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 = 12, name = "张三", address = "北京" }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .PostProcess<UserInfoDTO>((value, state) => { //在id编号前加上今年的年份 value.id = DateTime.Now.ToString("yyyy") + value.id; //实体的json格式 value.userJson = "{\"id\":\"" + value.id + "\",\"name\":\"" + value.name + "\"}"; return value; }) ); UserInfoDTO userdto = mapper.Map(user);
5.忽略掉某个字段的映射
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 = 12, name = "张三", address = "北京" }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .IgnoreMembers<UserInfo, UserInfoDTO>(new string[] { "name" }) ); UserInfoDTO userdto = mapper.Map(user);
6.给空元素赋默认值
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 = 12, name = "张三", address = null, godate = null }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() //如果日期为空设置为当前时间 .NullSubstitution<DateTime?, DateTime>((value) => DateTime.Now) //如果string类型为null赋值为“” .NullSubstitution<string, string>((value) => "") ); UserInfoDTO userdto = mapper.Map(user);
常用的就上面几点,对于更深入的。有待以后研究。。。。。