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;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值