MapperConfiguration.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Masuit.Tools.Mapping.Exceptions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. namespace Masuit.Tools.Mapping.Core
  7. {
  8. /// <summary>
  9. /// 主映射器
  10. /// </summary>
  11. /// <typeparam name="TSource">源类型</typeparam>
  12. /// <typeparam name="TDest">目标类型</typeparam>
  13. public class MapperConfiguration<TSource, TDest> : MapperConfigurationBase
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. protected readonly IList<Action<TSource, TDest>> actionsAfterMap;
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. public MapperConfiguration(string paramName, string mapperName = null) : base(typeof(TSource), typeof(TDest), paramName, mapperName)
  23. {
  24. actionsAfterMap = new List<Action<TSource, TDest>>();
  25. }
  26. /// <summary>
  27. /// 获取Lambda表达式树
  28. /// </summary>
  29. /// <returns></returns>
  30. public Expression<Func<TSource, TDest>> GetLambdaExpression()
  31. {
  32. MemberInitExpression exp = GetMemberInitExpression();
  33. return Expression.Lambda<Func<TSource, TDest>>(exp, paramClassSource);
  34. }
  35. /// <summary>
  36. /// 获取委托
  37. /// </summary>
  38. /// <returns></returns>
  39. public Func<TSource, TDest> GetFuncDelegate()
  40. {
  41. return (Func<TSource, TDest>)GetDelegate();
  42. }
  43. /// <summary>
  44. /// 映射成员
  45. /// </summary>
  46. /// <param name="getPropertySource">源类型</param>
  47. /// <param name="getPropertyDest">目标类型</param>
  48. /// <returns></returns>
  49. public MapperConfiguration<TSource, TDest> ForMember<TPropertySource, TPropertyDest>(Expression<Func<TSource, TPropertySource>> getPropertySource, Expression<Func<TDest, TPropertyDest>> getPropertyDest)
  50. {
  51. // 添加到映射列表并且可以继续操作
  52. ForMemberBase(getPropertySource.Body, getPropertyDest.Body, false);
  53. return this;
  54. }
  55. /// <summary>
  56. /// 映射成员
  57. /// </summary>
  58. /// <typeparam name="TPropertySource">属性源类型</typeparam>
  59. /// <typeparam name="TPropertyDest">属性目标类型</typeparam>
  60. /// <param name="getPropertySource">源类型</param>
  61. /// <param name="getPropertyDest">目标类型</param>
  62. /// <param name="checkIfNull">是否检查null值</param>
  63. /// <returns></returns>
  64. public MapperConfiguration<TSource, TDest> ForMember<TPropertySource, TPropertyDest>(Expression<Func<TSource, TPropertySource>> getPropertySource, Expression<Func<TDest, TPropertyDest>> getPropertyDest, bool checkIfNull)
  65. {
  66. // 添加到映射列表并且可以继续操作
  67. ForMemberBase(getPropertySource.Body, getPropertyDest.Body, checkIfNull);
  68. return this;
  69. }
  70. /// <summary>
  71. /// 映射成员
  72. /// </summary>
  73. /// <typeparam name="TPropertySource">属性源类型</typeparam>
  74. /// <typeparam name="TPropertyDest">属性目标类型</typeparam>
  75. /// <param name="getPropertySource">源类型</param>
  76. /// <param name="getPropertyDest">目标类型</param>
  77. /// <param name="mapperName">mapper别名</param>
  78. /// <returns></returns>
  79. public MapperConfiguration<TSource, TDest> ForMember<TPropertySource, TPropertyDest>(Expression<Func<TSource, TPropertySource>> getPropertySource, Expression<Func<TDest, TPropertyDest>> getPropertyDest, string mapperName)
  80. {
  81. // 添加到映射列表并且可以继续操作
  82. ForMemberBase(getPropertySource.Body, getPropertyDest.Body, true, mapperName);
  83. return this;
  84. }
  85. /// <summary>
  86. /// 忽略一些不需要映射的成员
  87. /// </summary>
  88. /// <param name="propertyDest">属性名</param>
  89. /// <returns></returns>
  90. public MapperConfiguration<TSource, TDest> Ignore<TProperty>(Expression<Func<TDest, TProperty>> propertyDest)
  91. {
  92. return IgnoreBase(propertyDest) as MapperConfiguration<TSource, TDest>;
  93. }
  94. /// <summary>
  95. /// 映射后要执行的操作
  96. /// </summary>
  97. /// <param name="actionAfterMap">映射后要执行的操作</param>
  98. /// <returns></returns>
  99. public MapperConfiguration<TSource, TDest> AfterMap(Action<TSource, TDest> actionAfterMap)
  100. {
  101. // 添加到映射列表并且可以继续操作
  102. actionsAfterMap.Add(actionAfterMap);
  103. return this;
  104. }
  105. /// <summary>
  106. /// 执行后续操作。
  107. /// </summary>
  108. /// <param name="source">源类型</param>
  109. /// <param name="dest">目标类型</param>
  110. public void ExecuteAfterActions(TSource source, TDest dest)
  111. {
  112. if (actionsAfterMap.Count > 0)
  113. {
  114. foreach (var action in actionsAfterMap)
  115. {
  116. if (action == null)
  117. {
  118. throw new NoActionAfterMappingException();
  119. }
  120. action(source, dest);
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// 反向映射
  126. /// </summary>
  127. /// <param name="name">mapper别名</param>
  128. /// <returns>
  129. /// 新的mapper对象
  130. /// </returns>
  131. /// <exception cref="MapperExistException"></exception>
  132. public MapperConfiguration<TDest, TSource> ReverseMap(string name = null)
  133. {
  134. MapperConfigurationBase map = GetMapper(typeof(TDest), typeof(TSource), false, name);
  135. if (map != null)
  136. {
  137. throw new MapperExistException(typeof(TDest), typeof(TSource));
  138. }
  139. string finalName = string.IsNullOrEmpty(name) ? "s" + (MapperConfigurationCollectionContainer.Instance.Count).ToString() : name;
  140. map = new MapperConfiguration<TDest, TSource>(finalName);
  141. MapperConfigurationCollectionContainer.Instance.Add(map);
  142. CreateCommonMember();
  143. // 现有属性的映射,并且创建反向关系
  144. foreach (var item in PropertiesMapping)
  145. {
  146. PropertyInfo propertyDest = GetPropertyInfo(item.Item1);
  147. if (propertyDest.CanWrite)
  148. {
  149. if (!string.IsNullOrEmpty(item.Item4))
  150. {
  151. //找到反向关系的mapper
  152. var reverseMapper = GetMapper(item.Item2.Type, item.Item1.Type, false);
  153. if (reverseMapper != null)
  154. {
  155. map.ForMemberBase(item.Item2, item.Item1, item.Item3, reverseMapper.Name);
  156. }
  157. }
  158. else
  159. {
  160. if (item.Item1.NodeType == ExpressionType.MemberAccess)
  161. {
  162. map.ForMemberBase(item.Item2, item.Item1, item.Item3, item.Item4);
  163. }
  164. }
  165. }
  166. }
  167. return (MapperConfiguration<TDest, TSource>)map;
  168. }
  169. /// <summary>
  170. /// 是否使用服务注入
  171. /// </summary>
  172. public MapperConfiguration<TSource, TDest> ConstructUsingServiceLocator()
  173. {
  174. UseServiceLocator = true;
  175. return this;
  176. }
  177. }
  178. }