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

/src/FluentNHibernate/MappingModel/MappedMembers.cs

http://github.com/jagregory/fluent-nhibernate
C# | 225 lines | 187 code | 38 blank | 0 comment | 42 complexity | fd67c960b581b531e75e0eac4d7919b1 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.IO;
  4. using System.Linq;
  5. using FluentNHibernate.MappingModel.ClassBased;
  6. using FluentNHibernate.Utils;
  7. using FluentNHibernate.Visitors;
  8. namespace FluentNHibernate.MappingModel
  9. {
  10. [Serializable]
  11. internal class MappedMembers : IMapping, IHasMappedMembers
  12. {
  13. public enum MappingType {
  14. Property,
  15. Collection,
  16. ManyToOne,
  17. IComponent,
  18. OneToOne,
  19. Any,
  20. Join,
  21. Filter,
  22. StoredProcedure,
  23. }
  24. private readonly List<Tuple<MappingType, IMapping>> orderedMappings;
  25. public MappedMembers()
  26. {
  27. orderedMappings = new List<Tuple<MappingType, IMapping>>();
  28. }
  29. public IEnumerable<PropertyMapping> Properties => orderedMappings.Where(x => x.Item1 == MappingType.Property).Select(x => x.Item2).Cast<PropertyMapping>();
  30. public IEnumerable<Collections.CollectionMapping> Collections => orderedMappings.Where(x => x.Item1 == MappingType.Collection).Select(x => x.Item2).Cast<Collections.CollectionMapping>();
  31. public IEnumerable<ManyToOneMapping> References => orderedMappings.Where(x => x.Item1 == MappingType.ManyToOne).Select(x => x.Item2).Cast<ManyToOneMapping>();
  32. public IEnumerable<IComponentMapping> Components => orderedMappings.Where(x => x.Item1 == MappingType.IComponent).Select(x => x.Item2).Cast<IComponentMapping>();
  33. public IEnumerable<OneToOneMapping> OneToOnes => orderedMappings.Where(x => x.Item1 == MappingType.OneToOne).Select(x => x.Item2).Cast<OneToOneMapping>();
  34. public IEnumerable<AnyMapping> Anys => orderedMappings.Where(x => x.Item1 == MappingType.Any).Select(x => x.Item2).Cast<AnyMapping>();
  35. public IEnumerable<JoinMapping> Joins => orderedMappings.Where(x => x.Item1 == MappingType.Join).Select(x => x.Item2).Cast<JoinMapping>();
  36. public IEnumerable<FilterMapping> Filters => orderedMappings.Where(x => x.Item1 == MappingType.Filter).Select(x => x.Item2).Cast<FilterMapping>();
  37. public IEnumerable<StoredProcedureMapping> StoredProcedures => orderedMappings.Where(x => x.Item1 == MappingType.StoredProcedure).Select(x => x.Item2).Cast<StoredProcedureMapping>();
  38. public void AddOrReplaceFilter(FilterMapping mapping)
  39. {
  40. AddOrReplaceMapping(mapping, MappingType.Filter, x => x.Name == mapping.Name);
  41. }
  42. public void AddProperty(PropertyMapping property)
  43. {
  44. if (Properties.Any(x => x.Name == property.Name))
  45. throw new InvalidOperationException("Tried to add property '" + property.Name + "' when already added.");
  46. AddMapping(property, MappingType.Property);
  47. }
  48. public void AddOrReplaceProperty(PropertyMapping mapping)
  49. {
  50. AddOrReplaceMapping(mapping, MappingType.Property, x => x.Name == mapping.Name);
  51. }
  52. public void AddCollection(Collections.CollectionMapping collection)
  53. {
  54. if (Collections.Any(x => x.Name == collection.Name))
  55. throw new InvalidOperationException("Tried to add collection '" + collection.Name + "' when already added.");
  56. AddMapping(collection, MappingType.Collection);
  57. }
  58. public void AddOrReplaceCollection(Collections.CollectionMapping mapping)
  59. {
  60. AddOrReplaceMapping(mapping, MappingType.Collection, x => x.Name == mapping.Name);
  61. }
  62. public void AddReference(ManyToOneMapping manyToOne)
  63. {
  64. if (References.Any(x => x.Name == manyToOne.Name))
  65. throw new InvalidOperationException("Tried to add many-to-one '" + manyToOne.Name + "' when already added.");
  66. AddMapping(manyToOne, MappingType.ManyToOne);
  67. }
  68. public void AddOrReplaceReference(ManyToOneMapping manyToOne)
  69. {
  70. AddOrReplaceMapping(manyToOne, MappingType.ManyToOne, x => x.Name == manyToOne.Name);
  71. }
  72. public void AddComponent(IComponentMapping componentMapping)
  73. {
  74. if (Components.Any(x => x.Name == componentMapping.Name))
  75. throw new InvalidOperationException("Tried to add component '" + componentMapping.Name + "' when already added.");
  76. AddMapping(componentMapping, MappingType.IComponent);
  77. }
  78. public void AddOrReplaceComponent(IComponentMapping componentMapping)
  79. {
  80. AddOrReplaceMapping(componentMapping, MappingType.IComponent, x => x.Name == componentMapping.Name);
  81. }
  82. public void AddOneToOne(OneToOneMapping mapping)
  83. {
  84. if (OneToOnes.Any(x => x.Name == mapping.Name))
  85. throw new InvalidOperationException("Tried to add one-to-one '" + mapping.Name + "' when already added.");
  86. AddMapping(mapping, MappingType.OneToOne);
  87. }
  88. public void AddOrReplaceOneToOne(OneToOneMapping mapping)
  89. {
  90. AddOrReplaceMapping(mapping, MappingType.OneToOne, x => x.Name == mapping.Name);
  91. }
  92. public void AddAny(AnyMapping mapping)
  93. {
  94. if (Anys.Any(x => x.Name == mapping.Name))
  95. throw new InvalidOperationException("Tried to add any '" + mapping.Name + "' when already added.");
  96. AddMapping(mapping, MappingType.Any);
  97. }
  98. public void AddOrReplaceAny(AnyMapping mapping)
  99. {
  100. AddOrReplaceMapping(mapping, MappingType.Any, x => x.Name == mapping.Name);
  101. }
  102. public void AddJoin(JoinMapping mapping)
  103. {
  104. if (Joins.Any(x => x.TableName == mapping.TableName))
  105. throw new InvalidOperationException("Tried to add join to table '" + mapping.TableName + "' when already added.");
  106. AddMapping(mapping, MappingType.Join);
  107. }
  108. public void AddFilter(FilterMapping mapping)
  109. {
  110. if (Filters.Any(x => x.Name == mapping.Name))
  111. throw new InvalidOperationException("Tried to add filter with name '" + mapping.Name + "' when already added.");
  112. AddMapping(mapping, MappingType.Filter);
  113. }
  114. public virtual void AcceptVisitor(IMappingModelVisitor visitor)
  115. {
  116. foreach(var mapping in orderedMappings)
  117. switch (mapping.Item1) {
  118. case MappingType.Property:
  119. visitor.Visit((PropertyMapping)mapping.Item2);
  120. break;
  121. case MappingType.Collection:
  122. visitor.Visit((Collections.CollectionMapping)mapping.Item2);
  123. break;
  124. case MappingType.ManyToOne:
  125. visitor.Visit((ManyToOneMapping)mapping.Item2);
  126. break;
  127. case MappingType.IComponent:
  128. visitor.Visit((IComponentMapping)mapping.Item2);
  129. break;
  130. case MappingType.OneToOne:
  131. visitor.Visit((OneToOneMapping)mapping.Item2);
  132. break;
  133. case MappingType.Any:
  134. visitor.Visit((AnyMapping)mapping.Item2);
  135. break;
  136. case MappingType.Join:
  137. visitor.Visit((JoinMapping)mapping.Item2);
  138. break;
  139. case MappingType.Filter:
  140. visitor.Visit((FilterMapping)mapping.Item2);
  141. break;
  142. case MappingType.StoredProcedure:
  143. visitor.Visit((StoredProcedureMapping)mapping.Item2);
  144. break;
  145. default:
  146. throw new Exception("Internal Error: unsupported mapping type " + mapping.Item1);
  147. }
  148. }
  149. public bool IsSpecified(string property)
  150. {
  151. return false;
  152. }
  153. public void Set(string attribute, int layer, object value)
  154. {}
  155. public void AddStoredProcedure(StoredProcedureMapping mapping)
  156. {
  157. AddMapping(mapping, MappingType.StoredProcedure);
  158. }
  159. public bool Equals(MappedMembers other)
  160. {
  161. if (ReferenceEquals(null, other)) return false;
  162. if (ReferenceEquals(this, other)) return true;
  163. return other.orderedMappings.ContentEquals(orderedMappings);
  164. }
  165. public override bool Equals(object obj)
  166. {
  167. if (ReferenceEquals(null, obj)) return false;
  168. if (ReferenceEquals(this, obj)) return true;
  169. if (obj.GetType() != typeof(MappedMembers)) return false;
  170. return Equals((MappedMembers)obj);
  171. }
  172. public override int GetHashCode() {
  173. return orderedMappings.GetHashCode();
  174. }
  175. private void AddMapping<TMapping>(TMapping mapping, MappingType mappingType) where TMapping : IMapping {
  176. orderedMappings.Add(Tuple.Create(mappingType, (IMapping)mapping));
  177. }
  178. private void AddOrReplaceMapping<TMapping>(TMapping mapping, MappingType mappingType, Predicate<TMapping> matchPredicate) {
  179. var newMapping = Tuple.Create(mappingType, (IMapping)mapping);
  180. var index = orderedMappings.FindIndex(x => x.Item1 == mappingType && matchPredicate((TMapping)x.Item2));
  181. if (index >= 0)
  182. orderedMappings[index] = newMapping;
  183. else
  184. orderedMappings.Add(newMapping);
  185. }
  186. }
  187. }