IProperty.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using InABox.Clients;
  2. using System;
  3. using System.Linq.Expressions;
  4. using System.Runtime.CompilerServices;
  5. namespace InABox.Core
  6. {
  7. public interface IProperty
  8. {
  9. string Class { get; set; }
  10. Type? ClassType { get; set; }
  11. string Name { get; set; }
  12. string Type { get; set; }
  13. Type PropertyType { get; set; }
  14. string Page { get; set; }
  15. /// <summary>
  16. /// Whether the property or any parents actually declares an editor.
  17. /// </summary>
  18. /// <remarks>
  19. /// If <c>false</c>, <see cref="Editor"/> will be a <see cref="NullEditor"/>.
  20. /// </remarks>
  21. bool HasEditor { get; set; }
  22. BaseEditor Editor { get; set; }
  23. long Sequence { get; set; }
  24. string Comment { get; set; }
  25. string Caption { get; set; }
  26. bool IsCalculated { get; }
  27. bool IsPersistable { get; }
  28. bool IsSerializable { get; }
  29. /// <summary>
  30. /// Returns <see langword="true"/> if this is a column to be stored in a database table.
  31. /// </summary>
  32. /// <remarks>
  33. /// This is equivalent to the property being a <see cref="CustomProperty"/> or a <see cref="StandardProperty"/> which
  34. /// is non-calculated, persistable, writeable and is a <i>local property</i>.
  35. /// </remarks>
  36. bool IsDBColumn { get; }
  37. /// <summary>
  38. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  39. /// If it is part of an <see cref="IEntityLink"/>, then it is only required if the <see cref="IEntityLink"/> property on the parent class
  40. /// also has <see cref="RequiredColumnAttribute"/>.
  41. /// </summary>
  42. bool Required { get; set; }
  43. /// <summary>
  44. /// Null if the <see cref="IProperty"/> is not loggable.<br/>
  45. /// An <see cref="IProperty"/> is loggable if it has the <see cref="LoggablePropertyAttribute"/> defined on it.<br/>
  46. /// If it is part of an <see cref="IEntityLink"/>, then it is only loggable if the <see cref="IEntityLink"/> property on the parent class
  47. /// also has <see cref="LoggablePropertyAttribute"/>.
  48. /// </summary>
  49. LoggablePropertyAttribute? Loggable { get; set; }
  50. IProperty? Parent { get; set; }
  51. bool IsEntityLink { get; set; }
  52. bool IsEnclosedEntity { get; set; }
  53. bool IsParent { get; set; }
  54. Expression Expression();
  55. Func<object, object> Getter();
  56. internal Func<object, object> NullSafeGetter();
  57. Action<object, object?> Setter();
  58. TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute;
  59. decimal PropertySequence();
  60. }
  61. public static class PropertyExtensions
  62. {
  63. public static bool HasAttribute<TAttribute>(this IProperty property) where TAttribute : Attribute
  64. => property.GetAttribute<TAttribute>() != null;
  65. /// <summary>
  66. /// Get the outermost parent property which has an editor.
  67. /// </summary>
  68. /// <param name="property"></param>
  69. /// <returns></returns>
  70. public static IProperty? GetParentWithEditor(this IProperty property)
  71. {
  72. if (property.Parent == null) return null;
  73. var parent = property.Parent.GetParentWithEditor();
  74. if (parent != null) return parent;
  75. if (property.Parent.HasEditor)
  76. {
  77. return property.Parent;
  78. }
  79. return null;
  80. }
  81. /// <summary>
  82. /// Gets the outermost parent property which matches the predicate.
  83. /// </summary>
  84. /// <param name="property"></param>
  85. /// <param name="predicate"></param>
  86. /// <returns></returns>
  87. public static IProperty? GetOuterParent(this IProperty property, Func<IProperty, bool> predicate)
  88. {
  89. if (property.Parent == null) return null;
  90. return property.Parent.GetOuterParent(predicate)
  91. ?? (predicate(property.Parent) ? property.Parent : null);
  92. }
  93. /// <summary>
  94. /// Gets the innermost parent property which matches the predicate.
  95. /// </summary>
  96. /// <param name="property"></param>
  97. /// <param name="predicate"></param>
  98. /// <returns></returns>
  99. public static IProperty? GetParent(this IProperty property, Func<IProperty, bool> predicate)
  100. {
  101. if (property.Parent == null) return null;
  102. if(predicate(property.Parent)) return property.Parent;
  103. return property.Parent.GetParent(predicate);
  104. }
  105. public static bool HasParentEditor(this IProperty property)
  106. {
  107. return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor());
  108. }
  109. public static bool HasParentEntityLink(this IProperty property)
  110. {
  111. return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink());
  112. }
  113. public static bool ShouldShowEditor(this IProperty property)
  114. {
  115. if (property.Parent == null)
  116. return true;
  117. if (property.HasParentEditor())
  118. return false;
  119. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID"))
  120. return false;
  121. if (property.Parent.HasParentEntityLink())
  122. return false;
  123. return true;
  124. }
  125. public static bool IsForeignKey(this IProperty property)
  126. {
  127. return property.Parent != null && property.Parent.IsEntityLink && property.Name.EndsWith(".ID");
  128. }
  129. }
  130. }