码迷,mamicode.com
首页 > 移动开发 > 详细

AutoMapper扩展

时间:2016-07-24 17:58:05      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:

扩展类:AutoMapExtention

技术分享
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace AutoMapper.Extention
{
    public static class AutoMapExtention
    {
        /// <summary>
        /// 类型映射
        /// </summary>
        /// <typeparam name="F">From</typeparam>
        /// <typeparam name="T">To</typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T MapTo<F, T>(this F source)
            where T : class
            where F : class
        {
            if (source == null) { return default(T); }
            Mapper.CreateMap<F, T>();
            return Mapper.Map<T>(source);
        }

        /// <summary>
        /// 集合映射
        /// </summary>
        /// <typeparam name="F">From</typeparam>
        /// <typeparam name="T">To</typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<T> MapTo<F, T>(this IEnumerable<F> source, bool ignoreNull = false)
            where T : class
            where F : class
        {
            if (source == null) { return new List<T> { }; }
            var m = Mapper.CreateMap<F, T>();
            if (ignoreNull)
            {
                m.ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));
            }
            return Mapper.Map<List<T>>(source);
        }

        /// <summary>
        /// 集合映射   指定映射规则
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="defaultValueSets">默认值</param>
        /// <returns></returns>
        public static List<T> MapTo<F, T>(this IEnumerable<F> source, Func<Dictionary<Expression<Func<T, object>>, Action<IMemberConfigurationExpression<F>>>> mapRuleSets)
            where T : class
            where F : class
        {
            if (source == null) { return new List<T> { }; }
            var m = Mapper.CreateMap<F, T>();
            if (mapRuleSets != null)
            {
                var arr = mapRuleSets();
                foreach (var item in arr)
                {
                    m.ForMember(item.Key, item.Value);
                }
            }
            return Mapper.Map<List<T>>(source);
        }
    }
}
View Code

数据库实体模型:

技术分享
    public class User
    {
        public string ID { get; set; }
        public int? SerialNumber { get; set; }
        public string RealName { get; set; }
        public int Sex { get; set; }
    }
View Code

DTO模型

技术分享
    public partial class UserDTO
    {
        public Guid ID { get; set; }
        public int? SerialNumber { get; set; }
        public string RealName { get; set; }
        public bool Sex { get; set; }
    }
View Code

客户端测试代码

技术分享
using AutoMapper;
using AutoMapper.Extention;
using Datebase.Entity;
using EsDataImporter.Model;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace EmitMapper
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = new User()
            {
                ID = Guid.NewGuid().ToString(),
                Sex = 1,
                RealName = "陈楚生",
                SerialNumber = 1
            };

            //单体映射
            var userDTO = user.MapTo<User, UserDTO>();

            List<User> users = new List<User>() { user };
            //集合映射并指定映射规则
            var userDTOs = users.MapTo<User, UserDTO>(() =>
            {
                var rules = new Dictionary<Expression<Func<UserDTO, object>>, Action<IMemberConfigurationExpression<User>>>();
                rules.Add(dst => dst.ID, opt => opt.MapFrom(src => Guid.Parse(src.ID)));
                rules.Add(dst => dst.RealName, opt => opt.MapFrom(src => src.RealName + "," + "Hello"));
                rules.Add(dst => dst.Sex, opt => opt.MapFrom(src => src.Sex == 1 ? true : false));
                rules.Add(dst => dst.SerialNumber, opt => opt.MapFrom(src => src.SerialNumber + 100));
                return rules;
            });

            Console.ReadLine();
        }
    }
}
View Code

 

AutoMapper扩展

标签:

原文地址:http://www.cnblogs.com/Jabben/p/5700986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!