IProperty.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 bool HasParentEditor(this IProperty property)
  46. {
  47. return property.Parent != null && (property.Parent.HasEditor || property.Parent.HasParentEditor());
  48. }
  49. public static bool HasParentEntityLink(this IProperty property)
  50. {
  51. return property.Parent != null && (property.Parent.IsEntityLink || property.Parent.HasParentEntityLink());
  52. }
  53. public static bool ShouldShowEditor(this IProperty property)
  54. {
  55. if (property.Parent == null)
  56. return true;
  57. if (property.HasParentEditor())
  58. return false;
  59. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID"))
  60. return false;
  61. if (property.Parent.HasParentEntityLink())
  62. return false;
  63. return true;
  64. }
  65. }
  66. }