IProperty.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// <summary>
  26. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  27. /// If it is part of an <see cref="IEntityLink"/>, then it is only required if the <see cref="IEntityLink"/> property on the parent class
  28. /// also has <see cref="RequiredColumnAttribute"/>.
  29. /// </summary>
  30. bool Required { get; set; }
  31. /// <summary>
  32. /// Null if the <see cref="IProperty"/> is not loggable.<br/>
  33. /// An <see cref="IProperty"/> is loggable if it has the <see cref="LoggablePropertyAttribute"/> defined on it.<br/>
  34. /// If it is part of an <see cref="IEntityLink"/>, then it is only loggable if the <see cref="IEntityLink"/> property on the parent class
  35. /// also has <see cref="LoggablePropertyAttribute"/>.
  36. /// </summary>
  37. LoggablePropertyAttribute? Loggable { get; set; }
  38. IProperty? Parent { get; set; }
  39. bool IsEntityLink { get; set; }
  40. bool IsEnclosedEntity { get; set; }
  41. bool IsParent { get; set; }
  42. Expression Expression();
  43. Func<object, object> Getter();
  44. Action<object, object?> Setter();
  45. TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute;
  46. decimal PropertySequence();
  47. }
  48. public static class PropertyExtensions
  49. {
  50. public static bool HasAttribute<TAttribute>(this IProperty property) where TAttribute : Attribute
  51. => property.GetAttribute<TAttribute>() != null;
  52. /// <summary>
  53. /// Get the outermost parent property which has an editor.
  54. /// </summary>
  55. /// <param name="property"></param>
  56. /// <returns></returns>
  57. public static IProperty? GetParentWithEditor(this IProperty property)
  58. {
  59. if (property.Parent == null) return null;
  60. var parent = property.Parent.GetParentWithEditor();
  61. if (parent != null) return parent;
  62. if (property.Parent.HasEditor)
  63. {
  64. return property.Parent;
  65. }
  66. return null;
  67. }
  68. /// <summary>
  69. /// Gets the outermost parent property which matches the predicate.
  70. /// </summary>
  71. /// <param name="property"></param>
  72. /// <param name="predicate"></param>
  73. /// <returns></returns>
  74. public static IProperty? GetOuterParent(this IProperty property, Func<IProperty, bool> predicate)
  75. {
  76. if (property.Parent == null) return null;
  77. return property.Parent.GetOuterParent(predicate)
  78. ?? (predicate(property.Parent) ? property.Parent : null);
  79. }
  80. /// <summary>
  81. /// Gets the innermost 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? GetParent(this IProperty property, Func<IProperty, bool> predicate)
  87. {
  88. if (property.Parent == null) return null;
  89. if(predicate(property.Parent)) return property.Parent;
  90. return property.Parent.GetParent(predicate);
  91. }
  92. public static bool HasParentEditor(this IProperty property)
  93. {
  94. return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor());
  95. }
  96. public static bool HasParentEntityLink(this IProperty property)
  97. {
  98. return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink());
  99. }
  100. public static bool ShouldShowEditor(this IProperty property)
  101. {
  102. if (property.Parent == null)
  103. return true;
  104. if (property.HasParentEditor())
  105. return false;
  106. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID"))
  107. return false;
  108. if (property.Parent.HasParentEntityLink())
  109. return false;
  110. return true;
  111. }
  112. }
  113. }