zoukankan      html  css  js  c++  java
  • EmitMapper系列之二:EmitMapper的使用小结

    EmitMapper的入门

    EmitMapper引用

    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);
    复制代码

    常用的就上面几点,对于更深入的。有待以后研究。。。。。

    本篇文章引用:园友(Arik lee) 的 http://www.cnblogs.com/ariklee/p/3833598.html,总结相对比较全

  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/lrzr/p/7515965.html
Copyright © 2011-2022 走看看