PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/SubClassPart.cs

http://github.com/jagregory/fluent-nhibernate
C# | 154 lines | 115 code | 28 blank | 11 comment | 2 complexity | aa4843f4ff2d12469750216cf3981350 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.Diagnostics;
  4. using FluentNHibernate.Mapping.Providers;
  5. using FluentNHibernate.MappingModel;
  6. using FluentNHibernate.MappingModel.ClassBased;
  7. using FluentNHibernate.Utils;
  8. namespace FluentNHibernate.Mapping
  9. {
  10. [Obsolete("REMOVE ME")]
  11. public class SubClassPart<TSubclass> : ClasslikeMapBase<TSubclass>, ISubclassMappingProvider
  12. {
  13. private readonly DiscriminatorPart parent;
  14. private readonly object discriminatorValue;
  15. private readonly MappingProviderStore providers;
  16. private readonly AttributeStore attributes = new AttributeStore();
  17. private readonly List<SubclassMapping> subclassMappings = new List<SubclassMapping>();
  18. private bool nextBool = true;
  19. public SubClassPart(DiscriminatorPart parent, object discriminatorValue)
  20. : this(parent, discriminatorValue, new MappingProviderStore())
  21. {}
  22. protected SubClassPart(DiscriminatorPart parent, object discriminatorValue, MappingProviderStore providers)
  23. : base(providers)
  24. {
  25. this.parent = parent;
  26. this.discriminatorValue = discriminatorValue;
  27. this.providers = providers;
  28. }
  29. SubclassMapping ISubclassMappingProvider.GetSubclassMapping()
  30. {
  31. var mapping = new SubclassMapping(SubclassType.Subclass, attributes.Clone());
  32. if (discriminatorValue != null)
  33. mapping.Set(x => x.DiscriminatorValue, Layer.Defaults, discriminatorValue);
  34. mapping.Set(x => x.Type, Layer.Defaults, typeof(TSubclass));
  35. mapping.Set(x => x.Name, Layer.Defaults, typeof(TSubclass).AssemblyQualifiedName);
  36. foreach (var property in providers.Properties)
  37. mapping.AddProperty(property.GetPropertyMapping());
  38. foreach (var component in providers.Components)
  39. mapping.AddComponent(component.GetComponentMapping());
  40. foreach (var oneToOne in providers.OneToOnes)
  41. mapping.AddOneToOne(oneToOne.GetOneToOneMapping());
  42. foreach (var collection in providers.Collections)
  43. mapping.AddCollection(collection.GetCollectionMapping());
  44. foreach (var reference in providers.References)
  45. mapping.AddReference(reference.GetManyToOneMapping());
  46. foreach (var any in providers.Anys)
  47. mapping.AddAny(any.GetAnyMapping());
  48. subclassMappings.Each(mapping.AddSubclass);
  49. return mapping;
  50. }
  51. public DiscriminatorPart SubClass<TChild>(object value, Action<SubClassPart<TChild>> action)
  52. {
  53. var subclass = new SubClassPart<TChild>(parent, value);
  54. action(subclass);
  55. subclassMappings.Add(((ISubclassMappingProvider)subclass).GetSubclassMapping());
  56. return parent;
  57. }
  58. public DiscriminatorPart SubClass<TChild>(Action<SubClassPart<TChild>> action)
  59. {
  60. return SubClass(null, action);
  61. }
  62. /// <summary>
  63. /// Sets whether this subclass is lazy loaded
  64. /// </summary>
  65. /// <returns></returns>
  66. public SubClassPart<TSubclass> LazyLoad()
  67. {
  68. attributes.Set("Lazy", Layer.UserSupplied, nextBool);
  69. nextBool = true;
  70. return this;
  71. }
  72. public SubClassPart<TSubclass> Proxy(Type type)
  73. {
  74. attributes.Set("Proxy", Layer.UserSupplied, type.AssemblyQualifiedName);
  75. return this;
  76. }
  77. public SubClassPart<TSubclass> Proxy<T>()
  78. {
  79. return Proxy(typeof(T));
  80. }
  81. public SubClassPart<TSubclass> DynamicUpdate()
  82. {
  83. attributes.Set("DynamicUpdate", Layer.UserSupplied, nextBool);
  84. nextBool = true;
  85. return this;
  86. }
  87. public SubClassPart<TSubclass> DynamicInsert()
  88. {
  89. attributes.Set("DynamicInsert", Layer.UserSupplied, nextBool);
  90. nextBool = true;
  91. return this;
  92. }
  93. public SubClassPart<TSubclass> SelectBeforeUpdate()
  94. {
  95. attributes.Set("SelectBeforeUpdate", Layer.UserSupplied, nextBool);
  96. nextBool = true;
  97. return this;
  98. }
  99. public SubClassPart<TSubclass> Abstract()
  100. {
  101. attributes.Set("Abstract", Layer.UserSupplied, nextBool);
  102. nextBool = true;
  103. return this;
  104. }
  105. /// <summary>
  106. /// Specifies an entity-name.
  107. /// </summary>
  108. /// <remarks>See http://nhforge.org/blogs/nhibernate/archive/2008/10/21/entity-name-in-action-a-strongly-typed-entity.aspx</remarks>
  109. public void EntityName(string entityName)
  110. {
  111. attributes.Set("EntityName", Layer.UserSupplied, entityName);
  112. }
  113. /// <summary>
  114. /// Inverts the next boolean
  115. /// </summary>
  116. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  117. public SubClassPart<TSubclass> Not
  118. {
  119. get
  120. {
  121. nextBool = !nextBool;
  122. return this;
  123. }
  124. }
  125. }
  126. }