PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/NaturalIdPart.cs

https://github.com/barriault/fluent-nhibernate
C# | 123 lines | 83 code | 18 blank | 22 comment | 0 complexity | b779f46bf946815555fba89b5218c14e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq.Expressions;
  5. using FluentNHibernate.Mapping.Providers;
  6. using FluentNHibernate.MappingModel;
  7. using FluentNHibernate.Utils;
  8. namespace FluentNHibernate.Mapping
  9. {
  10. public class NaturalIdPart<T> : INaturalIdMappingProvider
  11. {
  12. private readonly AttributeStore<NaturalIdMapping> attributes = new AttributeStore<NaturalIdMapping>();
  13. private readonly IList<PropertyMapping> properties = new List<PropertyMapping>();
  14. private readonly IList<ManyToOneMapping> manyToOnes = new List<ManyToOneMapping>();
  15. private bool nextBool = true;
  16. public NaturalIdPart() { }
  17. /// <summary>
  18. /// Defines a property to be used for this natural-id.
  19. /// </summary>
  20. /// <param name="expression">A member access lambda expression for the property</param>
  21. /// <returns>The natural id part fluent interface</returns>
  22. public NaturalIdPart<T> Property(Expression<Func<T, object>> expression)
  23. {
  24. var member = expression.ToMember();
  25. return Property(expression, member.Name);
  26. }
  27. /// <summary>
  28. /// Defines a property to be used for this natural-id with an explicit column name.
  29. /// </summary>
  30. /// <param name="expression">A member access lambda expression for the property</param>
  31. /// <param name="columnName">The column name in the database to use for this natural id, or null to use the property name</param>
  32. /// <returns>The natural id part fluent interface</returns>
  33. public NaturalIdPart<T> Property(Expression<Func<T, object>> expression, string columnName)
  34. {
  35. var member = expression.ToMember();
  36. return Property(member, columnName);
  37. }
  38. protected virtual NaturalIdPart<T> Property(Member member, string columnName)
  39. {
  40. var key = new PropertyMapping
  41. {
  42. Name = member.Name,
  43. Type = new TypeReference(member.PropertyType)
  44. };
  45. key.AddColumn(new ColumnMapping { Name = columnName });
  46. properties.Add(key);
  47. return this;
  48. }
  49. /// <summary>
  50. /// Defines a reference to be used as a many-to-one key for this natural-id with an explicit column name.
  51. /// </summary>
  52. /// <param name="expression">A member access lambda expression for the property</param>
  53. /// <returns>The natural ID part fluent interface</returns>
  54. public NaturalIdPart<T> Reference(Expression<Func<T, object>> expression)
  55. {
  56. var member = expression.ToMember();
  57. return Reference(expression, member.Name);
  58. }
  59. /// <summary>
  60. /// Defines a reference to be used as a many-to-one key for this natural-id with an explicit column name.
  61. /// </summary>
  62. /// <param name="expression">A member access lambda expression for the property</param>
  63. /// <param name="columnName">The column name in the database to use for this key, or null to use the property name</param>
  64. /// <returns>The natural id part fluent interface</returns>
  65. public NaturalIdPart<T> Reference(Expression<Func<T, object>> expression, string columnName)
  66. {
  67. var member = expression.ToMember();
  68. return Reference(expression, columnName);
  69. }
  70. protected virtual NaturalIdPart<T> Reference(Member member, string columnName)
  71. {
  72. var key = new ManyToOneMapping
  73. {
  74. Name = member.Name,
  75. Class = new TypeReference(member.PropertyType),
  76. ContainingEntityType = typeof(T)
  77. };
  78. key.AddColumn(new ColumnMapping { Name = columnName });
  79. manyToOnes.Add(key);
  80. return this;
  81. }
  82. public NaturalIdPart<T> ReadOnly()
  83. {
  84. attributes.Set(x => x.Mutable, nextBool);
  85. nextBool = true;
  86. return this;
  87. }
  88. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  89. public NaturalIdPart<T> Not
  90. {
  91. get
  92. {
  93. nextBool = false;
  94. return this;
  95. }
  96. }
  97. NaturalIdMapping INaturalIdMappingProvider.GetNaturalIdMapping()
  98. {
  99. var mapping = new NaturalIdMapping(attributes.CloneInner());
  100. properties.Each(mapping.AddProperty);
  101. manyToOnes.Each(mapping.AddReference);
  102. return mapping;
  103. }
  104. }
  105. }