IProperty.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /// <summary>
  24. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  25. /// If it is part of an <see cref="IEntityLink"/>, then it is only required if the <see cref="IEntityLink"/> property on the parent class
  26. /// also has <see cref="RequiredColumnAttribute"/>.
  27. /// </summary>
  28. bool Required { get; set; }
  29. /// <summary>
  30. /// Null if the <see cref="IProperty"/> is not loggable.<br/>
  31. /// An <see cref="IProperty"/> is loggable if it has the <see cref="LoggablePropertyAttribute"/> defined on it.<br/>
  32. /// If it is part of an <see cref="IEntityLink"/>, then it is only loggable if the <see cref="IEntityLink"/> property on the parent class
  33. /// also has <see cref="LoggablePropertyAttribute"/>.
  34. /// </summary>
  35. LoggablePropertyAttribute? Loggable { get; set; }
  36. IProperty? Parent { get; set; }
  37. bool IsEntityLink { get; set; }
  38. Expression Expression();
  39. Func<object, object> Getter();
  40. Action<object, object> Setter();
  41. }
  42. public static class PropertyExtensions
  43. {
  44. public static bool HasParentEditor(this IProperty property)
  45. {
  46. return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor());
  47. }
  48. private static bool HasParentEntityLink(this IProperty property)
  49. {
  50. return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink());
  51. }
  52. public static bool ShouldShowEditor(this IProperty property)
  53. {
  54. if (property.Parent == null)
  55. return true;
  56. if (property.HasParentEditor())
  57. return false;
  58. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID"))
  59. return false;
  60. if (property.Parent.HasParentEntityLink())
  61. return false;
  62. return true;
  63. }
  64. }
  65. }