using Masuit.Tools.Mapping.Core; using Masuit.Tools.Mapping.Exceptions; using System; using System.Linq.Expressions; namespace Masuit.Tools.Mapping { /// /// mapper的基类 /// public static class ExpressionMapper { private static Func _constructorFunc; private static bool _initialized; /// /// 映射指定的源。 /// /// 源类型 /// 目标类型 /// 源对象 /// 别名 /// /// 目标对象的新实例 /// public static TDest Map(this TSource source, string name = null) where TSource : class where TDest : class { if (source == null) { return null; } if (!_initialized) { Initialize(); } TDest result = null; MapperConfiguration mapper = GetMapper(name); Func query = mapper.GetFuncDelegate(); if (query != null) { result = query(source); // 映射后执行的操作 mapper.ExecuteAfterActions(source, result); } return result; } /// /// 将指定的源映射到目标。 /// /// 源类型 /// 目标类型 /// 源对象 /// 目标对象 /// 别名 public static void Map(this TSource source, TDest target, string name = null) where TSource : class where TDest : class { if (!_initialized) { Initialize(); } TDest result = null; MapperConfiguration mapper = GetMapper(name); Action query = mapper.GetDelegateForExistingTarget() as Action; if (query != null) { query(source, target); // 映射后执行的操作 mapper.ExecuteAfterActions(source, result); } } /// /// 获取查询表达式树 /// /// 源类型 /// 目标类型 /// public static Expression> GetQueryExpression() where TSource : class where TDest : class { return GetMapper().GetLambdaExpression(); } /// /// 创建mapper对象 /// /// 源类型 /// 目标类型 /// public static MapperConfiguration CreateMap(string name = null) where TSource : class where TDest : class { MapperConfigurationBase map = MapperConfigurationCollectionContainer.Instance.Find(typeof(TSource), typeof(TDest), name); if (map == null) { string finalName = string.IsNullOrEmpty(name) ? "s" + MapperConfigurationCollectionContainer.Instance.Count.ToString() : name; map = new MapperConfiguration(finalName); MapperConfigurationCollectionContainer.Instance.Add(map); } return map as MapperConfiguration; } /// /// 表示使用的依赖注入服务 /// /// 构造函数委托 public static void ConstructServicesUsing(Func constructor) { _constructorFunc = constructor; } /// /// 重置mapper /// public static void Reset() { MapperConfigurationCollectionContainer.Instance.Clear(); } /// /// 获取mapper实例 /// /// 源类型 /// 目标类型 /// 别名 /// public static MapperConfiguration GetMapper(string name = null) where TSource : class where TDest : class { return GetMapper(typeof(TSource), typeof(TDest), name) as MapperConfiguration; } /// /// 初始化mapper /// public static void Initialize() { MapperConfigurationCollectionContainer configRegister = MapperConfigurationCollectionContainer.Instance; foreach (var t in configRegister) { t.Initialize(_constructorFunc); } _initialized = true; } /// /// 获取mapper的委托 /// /// 源类型 /// 目标类型 /// public static Func GetQuery() where TSource : class where TDest : class { return GetMapper().GetFuncDelegate(); } /// /// 获取未映射的属性 /// public static PropertiesNotMapped GetPropertiesNotMapped(string name = null) where TSource : class where TDest : class { var mapper = GetMapper(name); return mapper.GetPropertiesNotMapped(); } internal static MapperConfigurationBase GetMapper(Type tSource, Type tDest, string name = null) { MapperConfigurationBase mapConfig = MapperConfigurationCollectionContainer.Instance.Find(tSource, tDest, name); if (mapConfig == null) { throw new NoFoundMapperException(tSource, tDest); } return mapConfig; } } }