IProperty.cs 5.4 KB

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