• 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);

    常用的就上面几点

            private IMapper _mapper;
            public IMapper Mapper
            {
                get
                {
                    if (null == _mapper)
                    {
                        _mapper = AssemblerIoc.GetMapper();
                    }
                    return _mapper;
                }
            }
        public class Mapper
        {
            public static IMapper _mapper;
            public static IMapper GetMapper
            {
                get
                {
                    if (_mapper == null)
                    {
                        _mapper = new MapperImpl();
                    }
                    return _mapper;
                }
            }
        }
        public interface IMapper
        {
            TDestination Map<TSource, TDestination>(TSource tSource);
    
            IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources);
            IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources);
        }
        public class MapperImpl : IMapper
        {
            public TDestination Map<TSource, TDestination>(TSource tSource)
            {
                if (tSource == null)
                    return default(TDestination);
    
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<TSource, TDestination>();
                return mapper.Map(tSource);
            }
    
            public IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources)
            {
                if (tSources == null)
                    return null;
    
                IList<TDestination> tDestinations = new List<TDestination>();
                foreach (var tSource in tSources)
                {
                    tDestinations.Add(Map<TSource, TDestination>(tSource));
                }
                return tDestinations;
            }
    
            public IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources)
            {
                if (tSources == null)
                    return null;
    
                IList<TDestination> tDestinations = new List<TDestination>();
                foreach (var tSource in tSources)
                {
                    tDestinations.Add(Map<TSource, TDestination>(tSource));
                }
                return tDestinations;
            }
        }
  • 相关阅读:
    Python PEP8 编码规范中文版
    MySQL分区表
    mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
    斐讯K2刷不死breed与第三方固件教程
    Mysql 多表连接查询 inner join 和 outer join 的使用
    mysql多表关联删除示例
    sublime Text 几款插件
    多进程vs多线程
    git 命令常用总结
    LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/5350471.html
Copyright © 2020-2023  润新知