标签:
扩展类: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); } } }
数据库实体模型:
public class User { public string ID { get; set; } public int? SerialNumber { get; set; } public string RealName { get; set; } public int Sex { get; set; } }
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; } }
客户端测试代码
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(); } } }
标签:
原文地址:http://www.cnblogs.com/Jabben/p/5700986.html