PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/MappingModel/Collections/CollectionMapping.cs

http://github.com/jagregory/fluent-nhibernate
C# | 302 lines | 243 code | 59 blank | 0 comment | 29 complexity | f966064513c17f9e1b07d42111017f1a MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using FluentNHibernate.Utils;
  5. using FluentNHibernate.Visitors;
  6. namespace FluentNHibernate.MappingModel.Collections
  7. {
  8. [Serializable]
  9. public class CollectionMapping : MappingBase, IRelationship
  10. {
  11. readonly AttributeStore attributes;
  12. readonly IList<FilterMapping> filters = new List<FilterMapping>();
  13. public Type ContainingEntityType { get; set; }
  14. public Member Member { get; set; }
  15. CollectionMapping(AttributeStore attributes)
  16. {
  17. Collection = Collection.Bag;
  18. this.attributes = attributes;
  19. }
  20. public IEnumerable<FilterMapping> Filters
  21. {
  22. get { return filters; }
  23. }
  24. public void AddFilter(FilterMapping mapping)
  25. {
  26. filters.Add(mapping);
  27. }
  28. public override void AcceptVisitor(IMappingModelVisitor visitor)
  29. {
  30. visitor.ProcessCollection(this);
  31. if (Key != null)
  32. visitor.Visit(Key);
  33. if (Index != null && (Collection == Collection.Array || Collection == Collection.List || Collection == Collection.Map))
  34. visitor.Visit(Index);
  35. if (Element != null)
  36. visitor.Visit(Element);
  37. if (CompositeElement != null)
  38. visitor.Visit(CompositeElement);
  39. if (Relationship != null)
  40. visitor.Visit(Relationship);
  41. foreach (var filter in Filters)
  42. visitor.Visit(filter);
  43. if (Cache != null)
  44. visitor.Visit(Cache);
  45. }
  46. public Type ChildType
  47. {
  48. get { return attributes.GetOrDefault<Type>("ChildType"); }
  49. }
  50. public IRelationship OtherSide { get; set; }
  51. public KeyMapping Key
  52. {
  53. get { return attributes.GetOrDefault<KeyMapping>("Key"); }
  54. }
  55. public ElementMapping Element
  56. {
  57. get { return attributes.GetOrDefault<ElementMapping>("Element"); }
  58. }
  59. public CompositeElementMapping CompositeElement
  60. {
  61. get { return attributes.GetOrDefault<CompositeElementMapping>("CompositeElement"); }
  62. }
  63. public CacheMapping Cache
  64. {
  65. get { return attributes.GetOrDefault<CacheMapping>("Cache"); }
  66. }
  67. public ICollectionRelationshipMapping Relationship
  68. {
  69. get { return attributes.GetOrDefault<ICollectionRelationshipMapping>("Relationship"); }
  70. }
  71. public bool Generic
  72. {
  73. get { return attributes.GetOrDefault<bool>("Generic"); }
  74. }
  75. public Lazy Lazy
  76. {
  77. get { return attributes.GetOrDefault<Lazy>("Lazy"); }
  78. }
  79. public bool Inverse
  80. {
  81. get { return attributes.GetOrDefault<bool>("Inverse"); }
  82. }
  83. public string Name
  84. {
  85. get { return attributes.GetOrDefault<string>("Name"); }
  86. }
  87. public string Access
  88. {
  89. get { return attributes.GetOrDefault<string>("Access"); }
  90. }
  91. public string TableName
  92. {
  93. get { return attributes.GetOrDefault<string>("TableName"); }
  94. }
  95. public string Schema
  96. {
  97. get { return attributes.GetOrDefault<string>("Schema"); }
  98. }
  99. public string Fetch
  100. {
  101. get { return attributes.GetOrDefault<string>("Fetch"); }
  102. }
  103. public string Cascade
  104. {
  105. get { return attributes.GetOrDefault<string>("Cascade"); }
  106. }
  107. public string Where
  108. {
  109. get { return attributes.GetOrDefault<string>("Where"); }
  110. }
  111. public bool Mutable
  112. {
  113. get { return attributes.GetOrDefault<bool>("Mutable"); }
  114. }
  115. public string Subselect
  116. {
  117. get { return attributes.GetOrDefault<string>("Subselect"); }
  118. }
  119. public TypeReference Persister
  120. {
  121. get { return attributes.GetOrDefault<TypeReference>("Persister"); }
  122. }
  123. public int BatchSize
  124. {
  125. get { return attributes.GetOrDefault<int>("BatchSize"); }
  126. }
  127. public string Check
  128. {
  129. get { return attributes.GetOrDefault<string>("Check"); }
  130. }
  131. public TypeReference CollectionType
  132. {
  133. get { return attributes.GetOrDefault<TypeReference>("CollectionType"); }
  134. }
  135. public bool OptimisticLock
  136. {
  137. get { return attributes.GetOrDefault<bool>("OptimisticLock"); }
  138. }
  139. public string OrderBy
  140. {
  141. get { return attributes.GetOrDefault<string>("OrderBy"); }
  142. }
  143. public Collection Collection { get; set; }
  144. public string Sort
  145. {
  146. get { return attributes.GetOrDefault<string>("Sort"); }
  147. }
  148. public IIndexMapping Index
  149. {
  150. get { return attributes.GetOrDefault<IIndexMapping>("Index"); }
  151. }
  152. public bool Equals(CollectionMapping other)
  153. {
  154. if (ReferenceEquals(null, other)) return false;
  155. if (ReferenceEquals(this, other)) return true;
  156. return Equals(other.attributes, attributes) &&
  157. other.filters.ContentEquals(filters) &&
  158. Equals(other.ContainingEntityType, ContainingEntityType)
  159. && Equals(other.Member, Member);
  160. }
  161. public override bool Equals(object obj)
  162. {
  163. if (ReferenceEquals(null, obj)) return false;
  164. if (ReferenceEquals(this, obj)) return true;
  165. if (obj.GetType() != typeof(CollectionMapping)) return false;
  166. return Equals((CollectionMapping)obj);
  167. }
  168. public override int GetHashCode()
  169. {
  170. unchecked
  171. {
  172. int result = (attributes != null ? attributes.GetHashCode() : 0);
  173. result = (result * 397) ^ (filters != null ? filters.GetHashCode() : 0);
  174. result = (result * 397) ^ (ContainingEntityType != null ? ContainingEntityType.GetHashCode() : 0);
  175. result = (result * 397) ^ (Member != null ? Member.GetHashCode() : 0);
  176. return result;
  177. }
  178. }
  179. public void Set<T>(Expression<Func<CollectionMapping, T>> expression, int layer, T value)
  180. {
  181. Set(expression.ToMember().Name, layer, value);
  182. }
  183. protected override void Set(string attribute, int layer, object value)
  184. {
  185. attributes.Set(attribute, layer, value);
  186. }
  187. public override bool IsSpecified(string attribute)
  188. {
  189. return attributes.IsSpecified(attribute);
  190. }
  191. public static CollectionMapping Array()
  192. {
  193. return Array(new AttributeStore());
  194. }
  195. public static CollectionMapping Array(AttributeStore underlyingStore)
  196. {
  197. return For(Collection.Array, underlyingStore);
  198. }
  199. public static CollectionMapping Bag()
  200. {
  201. return Bag(new AttributeStore());
  202. }
  203. public static CollectionMapping Bag(AttributeStore underlyingStore)
  204. {
  205. return For(Collection.Bag, underlyingStore);
  206. }
  207. public static CollectionMapping List()
  208. {
  209. return List(new AttributeStore());
  210. }
  211. public static CollectionMapping List(AttributeStore underlyingStore)
  212. {
  213. return For(Collection.List, underlyingStore);
  214. }
  215. public static CollectionMapping Map()
  216. {
  217. return Map(new AttributeStore());
  218. }
  219. public static CollectionMapping Map(AttributeStore underlyingStore)
  220. {
  221. return For(Collection.Map, underlyingStore);
  222. }
  223. public static CollectionMapping Set()
  224. {
  225. return Set(new AttributeStore());
  226. }
  227. public static CollectionMapping Set(AttributeStore underlyingStore)
  228. {
  229. return For(Collection.Set, underlyingStore);
  230. }
  231. public static CollectionMapping For(Collection collectionType)
  232. {
  233. return For(collectionType, new AttributeStore());
  234. }
  235. public static CollectionMapping For(Collection collectionType, AttributeStore underlyingStore)
  236. {
  237. return new CollectionMapping(underlyingStore)
  238. {
  239. Collection = collectionType
  240. };
  241. }
  242. }
  243. }