using Masuit.Tools.Mapping.Exceptions; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; namespace Masuit.Tools.Mapping.Core { /// /// 主映射器 /// /// 源类型 /// 目标类型 public class MapperConfiguration : MapperConfigurationBase { /// /// /// protected readonly IList> actionsAfterMap; /// /// 构造函数 /// public MapperConfiguration(string paramName, string mapperName = null) : base(typeof(TSource), typeof(TDest), paramName, mapperName) { actionsAfterMap = new List>(); } /// /// 获取Lambda表达式树 /// /// public Expression> GetLambdaExpression() { MemberInitExpression exp = GetMemberInitExpression(); return Expression.Lambda>(exp, paramClassSource); } /// /// 获取委托 /// /// public Func GetFuncDelegate() { return (Func)GetDelegate(); } /// /// 映射成员 /// /// 源类型 /// 目标类型 /// public MapperConfiguration ForMember(Expression> getPropertySource, Expression> getPropertyDest) { // 添加到映射列表并且可以继续操作 ForMemberBase(getPropertySource.Body, getPropertyDest.Body, false); return this; } /// /// 映射成员 /// /// 属性源类型 /// 属性目标类型 /// 源类型 /// 目标类型 /// 是否检查null值 /// public MapperConfiguration ForMember(Expression> getPropertySource, Expression> getPropertyDest, bool checkIfNull) { // 添加到映射列表并且可以继续操作 ForMemberBase(getPropertySource.Body, getPropertyDest.Body, checkIfNull); return this; } /// /// 映射成员 /// /// 属性源类型 /// 属性目标类型 /// 源类型 /// 目标类型 /// mapper别名 /// public MapperConfiguration ForMember(Expression> getPropertySource, Expression> getPropertyDest, string mapperName) { // 添加到映射列表并且可以继续操作 ForMemberBase(getPropertySource.Body, getPropertyDest.Body, true, mapperName); return this; } /// /// 忽略一些不需要映射的成员 /// /// 属性名 /// public MapperConfiguration Ignore(Expression> propertyDest) { return IgnoreBase(propertyDest) as MapperConfiguration; } /// /// 映射后要执行的操作 /// /// 映射后要执行的操作 /// public MapperConfiguration AfterMap(Action actionAfterMap) { // 添加到映射列表并且可以继续操作 actionsAfterMap.Add(actionAfterMap); return this; } /// /// 执行后续操作。 /// /// 源类型 /// 目标类型 public void ExecuteAfterActions(TSource source, TDest dest) { if (actionsAfterMap.Count > 0) { foreach (var action in actionsAfterMap) { if (action == null) { throw new NoActionAfterMappingException(); } action(source, dest); } } } /// /// 反向映射 /// /// mapper别名 /// /// 新的mapper对象 /// /// public MapperConfiguration ReverseMap(string name = null) { MapperConfigurationBase map = GetMapper(typeof(TDest), typeof(TSource), false, name); if (map != null) { throw new MapperExistException(typeof(TDest), typeof(TSource)); } string finalName = string.IsNullOrEmpty(name) ? "s" + (MapperConfigurationCollectionContainer.Instance.Count).ToString() : name; map = new MapperConfiguration(finalName); MapperConfigurationCollectionContainer.Instance.Add(map); CreateCommonMember(); // 现有属性的映射,并且创建反向关系 foreach (var item in PropertiesMapping) { PropertyInfo propertyDest = GetPropertyInfo(item.Item1); if (propertyDest.CanWrite) { if (!string.IsNullOrEmpty(item.Item4)) { //找到反向关系的mapper var reverseMapper = GetMapper(item.Item2.Type, item.Item1.Type, false); if (reverseMapper != null) { map.ForMemberBase(item.Item2, item.Item1, item.Item3, reverseMapper.Name); } } else { if (item.Item1.NodeType == ExpressionType.MemberAccess) { map.ForMemberBase(item.Item2, item.Item1, item.Item3, item.Item4); } } } } return (MapperConfiguration)map; } /// /// 是否使用服务注入 /// public MapperConfiguration ConstructUsingServiceLocator() { UseServiceLocator = true; return this; } } }