IProperty.cs 4.8 KB

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