IProperty.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. string Name { get; set; }
  10. string Type { get; set; }
  11. Type PropertyType { get; set; }
  12. string Page { get; set; }
  13. /// <summary>
  14. /// Whether the property or any parents actually declares an editor.
  15. /// </summary>
  16. /// <remarks>
  17. /// If <c>false</c>, <see cref="Editor"/> will be a <see cref="NullEditor"/>.
  18. /// </remarks>
  19. bool HasEditor { get; set; }
  20. BaseEditor Editor { get; set; }
  21. long Sequence { get; set; }
  22. string Caption { get; set; }
  23. bool IsCalculated { get; }
  24. /// <summary>
  25. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  26. /// If it is part of an <see cref="IEntityLink"/>, then it is only required if the <see cref="IEntityLink"/> property on the parent class
  27. /// also has <see cref="RequiredColumnAttribute"/>.
  28. /// </summary>
  29. bool Required { get; set; }
  30. /// <summary>
  31. /// Null if the <see cref="IProperty"/> is not loggable.<br/>
  32. /// An <see cref="IProperty"/> is loggable if it has the <see cref="LoggablePropertyAttribute"/> defined on it.<br/>
  33. /// If it is part of an <see cref="IEntityLink"/>, then it is only loggable if the <see cref="IEntityLink"/> property on the parent class
  34. /// also has <see cref="LoggablePropertyAttribute"/>.
  35. /// </summary>
  36. LoggablePropertyAttribute? Loggable { get; set; }
  37. IProperty? Parent { get; set; }
  38. bool IsEntityLink { get; set; }
  39. Expression Expression();
  40. Func<object, object> Getter();
  41. Action<object, object> Setter();
  42. }
  43. public static class PropertyExtensions
  44. {
  45. public static IProperty? GetParentWithEditor(this IProperty property)
  46. {
  47. if (property.Parent == null) return null;
  48. var parent = property.Parent.GetParentWithEditor();
  49. if (parent != null) return parent;
  50. if (property.Parent.HasEditor)
  51. {
  52. return property.Parent;
  53. }
  54. return null;
  55. }
  56. public static bool HasParentEditor(this IProperty property)
  57. {
  58. return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor());
  59. }
  60. public static bool HasParentEntityLink(this IProperty property)
  61. {
  62. return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink());
  63. }
  64. public static bool ShouldShowEditor(this IProperty property)
  65. {
  66. if (property.Parent == null)
  67. return true;
  68. if (property.HasParentEditor())
  69. return false;
  70. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID"))
  71. return false;
  72. if (property.Parent.HasParentEntityLink())
  73. return false;
  74. return true;
  75. }
  76. }
  77. }