PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/ManyToOnePart.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 223 lines | 178 code | 38 blank | 7 comment | 4 complexity | bebb2ffb8f0848a2d28a5750df52f8d6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using FluentNHibernate.Mapping.Providers;
  7. using FluentNHibernate.MappingModel;
  8. using FluentNHibernate.Utils;
  9. namespace FluentNHibernate.Mapping
  10. {
  11. public class ManyToOnePart<TOther> : IManyToOneMappingProvider
  12. {
  13. private readonly AccessStrategyBuilder<ManyToOnePart<TOther>> access;
  14. private readonly FetchTypeExpression<ManyToOnePart<TOther>> fetch;
  15. private readonly NotFoundExpression<ManyToOnePart<TOther>> notFound;
  16. private readonly CascadeExpression<ManyToOnePart<TOther>> cascade;
  17. private readonly IList<string> columns = new List<string>();
  18. private bool nextBool = true;
  19. private readonly AttributeStore<ManyToOneMapping> attributes = new AttributeStore<ManyToOneMapping>();
  20. private readonly AttributeStore<ColumnMapping> columnAttributes = new AttributeStore<ColumnMapping>();
  21. private readonly Type entity;
  22. private readonly Member property;
  23. public ManyToOnePart(Type entity, Member property)
  24. {
  25. this.entity = entity;
  26. this.property = property;
  27. access = new AccessStrategyBuilder<ManyToOnePart<TOther>>(this, value => attributes.Set(x => x.Access, value));
  28. fetch = new FetchTypeExpression<ManyToOnePart<TOther>>(this, value => attributes.Set(x => x.Fetch, value));
  29. cascade = new CascadeExpression<ManyToOnePart<TOther>>(this, value => attributes.Set(x => x.Cascade, value));
  30. notFound = new NotFoundExpression<ManyToOnePart<TOther>>(this, value => attributes.Set(x => x.NotFound, value));
  31. }
  32. ManyToOneMapping IManyToOneMappingProvider.GetManyToOneMapping()
  33. {
  34. var mapping = new ManyToOneMapping(attributes.CloneInner());
  35. mapping.ContainingEntityType = entity;
  36. mapping.Member = property;
  37. if (!mapping.IsSpecified("Name"))
  38. mapping.Name = property.Name;
  39. if (!mapping.IsSpecified("Class"))
  40. mapping.SetDefaultValue(x => x.Class, new TypeReference(typeof(TOther)));
  41. if (columns.Count == 0)
  42. mapping.AddDefaultColumn(CreateColumn(property.Name + "_id"));
  43. foreach (var column in columns)
  44. {
  45. var columnMapping = CreateColumn(column);
  46. mapping.AddColumn(columnMapping);
  47. }
  48. return mapping;
  49. }
  50. private ColumnMapping CreateColumn(string column)
  51. {
  52. return new ColumnMapping(columnAttributes.CloneInner()) { Name = column };
  53. }
  54. public FetchTypeExpression<ManyToOnePart<TOther>> Fetch
  55. {
  56. get { return fetch; }
  57. }
  58. public NotFoundExpression<ManyToOnePart<TOther>> NotFound
  59. {
  60. get { return notFound; }
  61. }
  62. public ManyToOnePart<TOther> Unique()
  63. {
  64. columnAttributes.Set(x => x.Unique, nextBool);
  65. nextBool = true;
  66. return this;
  67. }
  68. /// <summary>
  69. /// Specifies the name of a multi-column unique constraint.
  70. /// </summary>
  71. /// <param name="keyName">Name of constraint</param>
  72. public ManyToOnePart<TOther> UniqueKey(string keyName)
  73. {
  74. columnAttributes.Set(x => x.UniqueKey, keyName);
  75. return this;
  76. }
  77. public ManyToOnePart<TOther> Index(string indexName)
  78. {
  79. columnAttributes.Set(x => x.Index, indexName);
  80. return this;
  81. }
  82. public ManyToOnePart<TOther> Class<T>()
  83. {
  84. return Class(typeof(T));
  85. }
  86. public ManyToOnePart<TOther> Class(Type type)
  87. {
  88. attributes.Set(x => x.Class, new TypeReference(type));
  89. return this;
  90. }
  91. public ManyToOnePart<TOther> ReadOnly()
  92. {
  93. attributes.Set(x => x.Insert, !nextBool);
  94. attributes.Set(x => x.Update, !nextBool);
  95. nextBool = true;
  96. return this;
  97. }
  98. public ManyToOnePart<TOther> LazyLoad()
  99. {
  100. attributes.Set(x => x.Lazy, nextBool);
  101. nextBool = true;
  102. return this;
  103. }
  104. public ManyToOnePart<TOther> ForeignKey()
  105. {
  106. return ForeignKey(string.Format("FK_{0}To{1}", property.DeclaringType.Name, property.Name));
  107. }
  108. public ManyToOnePart<TOther> ForeignKey(string foreignKeyName)
  109. {
  110. attributes.Set(x => x.ForeignKey, foreignKeyName);
  111. return this;
  112. }
  113. public ManyToOnePart<TOther> Insert()
  114. {
  115. attributes.Set(x => x.Insert, nextBool);
  116. nextBool = true;
  117. return this;
  118. }
  119. public ManyToOnePart<TOther> Update()
  120. {
  121. attributes.Set(x => x.Update, nextBool);
  122. nextBool = true;
  123. return this;
  124. }
  125. public ManyToOnePart<TOther> Columns(params string[] columns)
  126. {
  127. foreach (var column in columns)
  128. {
  129. this.columns.Add(column);
  130. }
  131. return this;
  132. }
  133. public ManyToOnePart<TOther> Columns(params Expression<Func<TOther, object>>[] columns)
  134. {
  135. foreach (var expression in columns)
  136. {
  137. var property = ReflectionHelper.GetProperty(expression).ToMember();
  138. Columns(property.Name);
  139. }
  140. return this;
  141. }
  142. public CascadeExpression<ManyToOnePart<TOther>> Cascade
  143. {
  144. get { return cascade; }
  145. }
  146. public ManyToOnePart<TOther> Column(string name)
  147. {
  148. columns.Clear();
  149. columns.Add(name);
  150. return this;
  151. }
  152. public ManyToOnePart<TOther> PropertyRef(Expression<Func<TOther, object>> propertyRef)
  153. {
  154. var property = ReflectionHelper.GetProperty(propertyRef);
  155. return PropertyRef(property.Name);
  156. }
  157. public ManyToOnePart<TOther> PropertyRef(string property)
  158. {
  159. attributes.Set(x => x.PropertyRef, property);
  160. return this;
  161. }
  162. public ManyToOnePart<TOther> Nullable()
  163. {
  164. columnAttributes.Set(x => x.NotNull, !nextBool);
  165. nextBool = true;
  166. return this;
  167. }
  168. public AccessStrategyBuilder<ManyToOnePart<TOther>> Access
  169. {
  170. get { return access; }
  171. }
  172. /// <summary>
  173. /// Inverts the next boolean
  174. /// </summary>
  175. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  176. public ManyToOnePart<TOther> Not
  177. {
  178. get
  179. {
  180. nextBool = !nextBool;
  181. return this;
  182. }
  183. }
  184. }
  185. }