PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/ClasslikeMapBase.cs

https://github.com/signoredems/fluent-nhibernate
C# | 321 lines | 202 code | 62 blank | 57 comment | 3 complexity | 64d0828e6c39b5df24f80c99343a927a MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using FluentNHibernate.Mapping.Providers;
  7. using FluentNHibernate.Utils;
  8. namespace FluentNHibernate.Mapping
  9. {
  10. public abstract class ClasslikeMapBase<T>
  11. {
  12. protected readonly IList<IPropertyMappingProvider> properties = new List<IPropertyMappingProvider>();
  13. protected readonly IList<IComponentMappingProvider> components = new List<IComponentMappingProvider>();
  14. protected readonly IList<IOneToOneMappingProvider> oneToOnes = new List<IOneToOneMappingProvider>();
  15. protected readonly Dictionary<Type, ISubclassMappingProvider> subclasses = new Dictionary<Type, ISubclassMappingProvider>();
  16. protected readonly IList<ICollectionMappingProvider> collections = new List<ICollectionMappingProvider>();
  17. protected readonly IList<IManyToOneMappingProvider> references = new List<IManyToOneMappingProvider>();
  18. protected readonly IList<IAnyMappingProvider> anys = new List<IAnyMappingProvider>();
  19. protected readonly IList<IFilterMappingProvider> filters = new List<IFilterMappingProvider>();
  20. protected readonly IList<IStoredProcedureMappingProvider> storedProcedures = new List<IStoredProcedureMappingProvider>();
  21. public PropertyPart Map(Expression<Func<T, object>> expression)
  22. {
  23. return Map(expression, null);
  24. }
  25. public PropertyPart Map(Expression<Func<T, object>> expression, string columnName)
  26. {
  27. return Map(ReflectionHelper.GetProperty(expression).ToMember(), columnName);
  28. }
  29. protected virtual PropertyPart Map(Member property, string columnName)
  30. {
  31. var propertyMap = new PropertyPart(property, typeof(T));
  32. if (!string.IsNullOrEmpty(columnName))
  33. propertyMap.Column(columnName);
  34. properties.Add(propertyMap);
  35. return propertyMap;
  36. }
  37. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> expression)
  38. {
  39. return References(expression, null);
  40. }
  41. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> expression, string columnName)
  42. {
  43. return References<TOther>(ReflectionHelper.GetProperty(expression).ToMember(), columnName);
  44. }
  45. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, object>> expression)
  46. {
  47. return References<TOther>(expression, null);
  48. }
  49. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, object>> expression, string columnName)
  50. {
  51. return References<TOther>(ReflectionHelper.GetProperty(expression).ToMember(), columnName);
  52. }
  53. protected virtual ManyToOnePart<TOther> References<TOther>(Member property, string columnName)
  54. {
  55. var part = new ManyToOnePart<TOther>(EntityType, property);
  56. if (columnName != null)
  57. part.Column(columnName);
  58. references.Add(part);
  59. return part;
  60. }
  61. public AnyPart<TOther> ReferencesAny<TOther>(Expression<Func<T, TOther>> expression)
  62. {
  63. return ReferencesAny<TOther>(ReflectionHelper.GetProperty(expression).ToMember());
  64. }
  65. protected virtual AnyPart<TOther> ReferencesAny<TOther>(Member property)
  66. {
  67. var part = new AnyPart<TOther>(typeof(T), property);
  68. anys.Add(part);
  69. return part;
  70. }
  71. public OneToOnePart<TOther> HasOne<TOther>(Expression<Func<T, Object>> expression)
  72. {
  73. return HasOne<TOther>(ReflectionHelper.GetProperty(expression).ToMember());
  74. }
  75. public OneToOnePart<TOther> HasOne<TOther>(Expression<Func<T, TOther>> expression)
  76. {
  77. return HasOne<TOther>(ReflectionHelper.GetProperty(expression).ToMember());
  78. }
  79. protected virtual OneToOnePart<TOther> HasOne<TOther>(Member property)
  80. {
  81. var part = new OneToOnePart<TOther>(EntityType, property);
  82. oneToOnes.Add(part);
  83. return part;
  84. }
  85. public DynamicComponentPart<IDictionary> DynamicComponent(Expression<Func<T, IDictionary>> expression, Action<DynamicComponentPart<IDictionary>> action)
  86. {
  87. return DynamicComponent(ReflectionHelper.GetProperty(expression).ToMember(), action);
  88. }
  89. protected DynamicComponentPart<IDictionary> DynamicComponent(Member property, Action<DynamicComponentPart<IDictionary>> action)
  90. {
  91. var part = new DynamicComponentPart<IDictionary>(typeof(T), property);
  92. action(part);
  93. components.Add(part);
  94. return part;
  95. }
  96. /// <summary>
  97. /// Maps a component
  98. /// </summary>
  99. /// <typeparam name="TComponent">Type of component</typeparam>
  100. /// <param name="expression">Component property</param>
  101. /// <param name="action">Component mapping</param>
  102. public ComponentPart<TComponent> Component<TComponent>(Expression<Func<T, TComponent>> expression, Action<ComponentPart<TComponent>> action)
  103. {
  104. return Component(ReflectionHelper.GetProperty(expression).ToMember(), action);
  105. }
  106. /// <summary>
  107. /// Maps a component
  108. /// </summary>
  109. /// <typeparam name="TComponent">Type of component</typeparam>
  110. /// <param name="expression">Component property</param>
  111. /// <param name="action">Component mapping</param>
  112. public ComponentPart<TComponent> Component<TComponent>(Expression<Func<T, object>> expression, Action<ComponentPart<TComponent>> action)
  113. {
  114. return Component(ReflectionHelper.GetProperty(expression).ToMember(), action);
  115. }
  116. protected virtual ComponentPart<TComponent> Component<TComponent>(Member property, Action<ComponentPart<TComponent>> action)
  117. {
  118. var part = new ComponentPart<TComponent>(typeof(T), property);
  119. action(part);
  120. components.Add(part);
  121. return part;
  122. }
  123. /// <summary>
  124. /// CreateProperties a one-to-many relationship
  125. /// </summary>
  126. /// <typeparam name="TChild">Child object type</typeparam>
  127. /// <typeparam name="TReturn">Property return type</typeparam>
  128. /// <param name="expression">Expression to get property from</param>
  129. /// <returns>one-to-many part</returns>
  130. private OneToManyPart<TChild> MapHasMany<TChild, TReturn>(Expression<Func<T, TReturn>> expression)
  131. {
  132. return ReflectionHelper.IsMethodExpression(expression)
  133. ? HasMany<TChild>(ReflectionHelper.GetMethod(expression))
  134. : HasMany<TChild>(ReflectionHelper.GetProperty(expression).ToMember());
  135. }
  136. protected virtual OneToManyPart<TChild> HasMany<TChild>(MethodInfo method)
  137. {
  138. var part = new OneToManyPart<TChild>(EntityType, method);
  139. collections.Add(part);
  140. return part;
  141. }
  142. protected virtual OneToManyPart<TChild> HasMany<TChild>(Member property)
  143. {
  144. var part = new OneToManyPart<TChild>(EntityType, property);
  145. collections.Add(part);
  146. return part;
  147. }
  148. /// <summary>
  149. /// CreateProperties a one-to-many relationship
  150. /// </summary>
  151. /// <typeparam name="TChild">Child object type</typeparam>
  152. /// <param name="expression">Expression to get property from</param>
  153. /// <returns>one-to-many part</returns>
  154. public OneToManyPart<TChild> HasMany<TChild>(Expression<Func<T, IEnumerable<TChild>>> expression)
  155. {
  156. return MapHasMany<TChild, IEnumerable<TChild>>(expression);
  157. }
  158. /// <summary>
  159. /// CreateProperties a one-to-many relationship with a IDictionary
  160. /// </summary>
  161. /// <typeparam name="TKey">Dictionary key type</typeparam>
  162. /// <typeparam name="TChild">Child object type / Dictionary value type</typeparam>
  163. /// <param name="expression">Expression to get property from</param>
  164. /// <returns>one-to-many part</returns>
  165. public OneToManyPart<TChild> HasMany<TKey, TChild>(Expression<Func<T, IDictionary<TKey, TChild>>> expression)
  166. {
  167. return MapHasMany<TChild, IDictionary<TKey, TChild>>(expression);
  168. }
  169. /// <summary>
  170. /// CreateProperties a one-to-many relationship
  171. /// </summary>
  172. /// <typeparam name="TChild">Child object type</typeparam>
  173. /// <param name="expression">Expression to get property from</param>
  174. /// <returns>one-to-many part</returns>
  175. public OneToManyPart<TChild> HasMany<TChild>(Expression<Func<T, object>> expression)
  176. {
  177. return MapHasMany<TChild, object>(expression);
  178. }
  179. /// <summary>
  180. /// CreateProperties a many-to-many relationship
  181. /// </summary>
  182. /// <typeparam name="TChild">Child object type</typeparam>
  183. /// <typeparam name="TReturn">Property return type</typeparam>
  184. /// <param name="expression">Expression to get property from</param>
  185. /// <returns>many-to-many part</returns>
  186. private ManyToManyPart<TChild> MapHasManyToMany<TChild, TReturn>(Expression<Func<T, TReturn>> expression)
  187. {
  188. return ReflectionHelper.IsMethodExpression(expression)
  189. ? HasManyToMany<TChild>(ReflectionHelper.GetMethod(expression))
  190. : HasManyToMany<TChild>(ReflectionHelper.GetProperty(expression).ToMember());
  191. }
  192. protected virtual ManyToManyPart<TChild> HasManyToMany<TChild>(MethodInfo method)
  193. {
  194. var part = new ManyToManyPart<TChild>(EntityType, method);
  195. collections.Add(part);
  196. return part;
  197. }
  198. protected virtual ManyToManyPart<TChild> HasManyToMany<TChild>(Member property)
  199. {
  200. var part = new ManyToManyPart<TChild>(EntityType, property);
  201. collections.Add(part);
  202. return part;
  203. }
  204. /// <summary>
  205. /// CreateProperties a many-to-many relationship
  206. /// </summary>
  207. /// <typeparam name="TChild">Child object type</typeparam>
  208. /// <param name="expression">Expression to get property from</param>
  209. /// <returns>many-to-many part</returns>
  210. public ManyToManyPart<TChild> HasManyToMany<TChild>(Expression<Func<T, IEnumerable<TChild>>> expression)
  211. {
  212. return MapHasManyToMany<TChild, IEnumerable<TChild>>(expression);
  213. }
  214. /// <summary>
  215. /// CreateProperties a many-to-many relationship
  216. /// </summary>
  217. /// <typeparam name="TChild">Child object type</typeparam>
  218. /// <param name="expression">Expression to get property from</param>
  219. /// <returns>many-to-many part</returns>
  220. public ManyToManyPart<TChild> HasManyToMany<TChild>(Expression<Func<T, object>> expression)
  221. {
  222. return MapHasManyToMany<TChild, object>(expression);
  223. }
  224. public StoredProcedurePart SqlInsert(string innerText)
  225. {
  226. return StoredProcedure("sql-insert", innerText);
  227. }
  228. public StoredProcedurePart SqlUpdate(string innerText)
  229. {
  230. return StoredProcedure("sql-update", innerText);
  231. }
  232. public StoredProcedurePart SqlDelete(string innerText)
  233. {
  234. return StoredProcedure("sql-delete", innerText);
  235. }
  236. public StoredProcedurePart SqlDeleteAll(string innerText)
  237. {
  238. return StoredProcedure("sql-delete-all", innerText);
  239. }
  240. protected StoredProcedurePart StoredProcedure(string element, string innerText)
  241. {
  242. var part = new StoredProcedurePart(element, innerText);
  243. storedProcedures.Add(part);
  244. return part;
  245. }
  246. protected virtual IEnumerable<IPropertyMappingProvider> Properties
  247. {
  248. get { return properties; }
  249. }
  250. protected virtual IEnumerable<IComponentMappingProvider> Components
  251. {
  252. get { return components; }
  253. }
  254. public Type EntityType
  255. {
  256. get { return typeof(T); }
  257. }
  258. }
  259. }