IProperty.cs 5.3 KB

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