Shell.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. namespace InABox.Avalonia
  6. {
  7. public abstract class Shell<TParent,TEntity> : INotifyPropertyChanged, IShell, IShell<TEntity>
  8. where TParent : ICoreRepository
  9. where TEntity : Entity, IPersistent, IRemotable, new()
  10. {
  11. #region INotifyPropertyChanged
  12. public new event PropertyChangedEventHandler? PropertyChanged;
  13. protected void DoPropertyChanged(string propertyName)
  14. {
  15. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  16. }
  17. protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  18. {
  19. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  20. field = value;
  21. DoPropertyChanged(propertyName);
  22. return true;
  23. }
  24. #endregion
  25. private TEntity _entity;
  26. private TEntity CheckEntity()
  27. {
  28. _entity ??= Row.ToObject<TEntity>();
  29. return _entity;
  30. }
  31. public TEntity Entity => CheckEntity();
  32. protected virtual void RowChanged()
  33. {
  34. }
  35. public bool IsChanged() => _entity?.IsChanged() ?? false;
  36. public virtual void Save(string auditmessage)
  37. {
  38. if (_entity != null)
  39. new Client<TEntity>().Save(_entity, auditmessage);
  40. }
  41. public void Cancel()
  42. {
  43. _entity?.CancelChanges();
  44. _entity = null;
  45. }
  46. private CoreRow _row;
  47. public CoreRow Row
  48. {
  49. get => _row;
  50. set
  51. {
  52. _row = value;
  53. RowChanged();
  54. }
  55. }
  56. public TParent Parent { get; set; }
  57. ICoreRepository IShell.Parent => this.Parent;
  58. public Guid ID => Get<Guid>();
  59. public virtual string[] TextSearchValues() => [];
  60. public bool Match(string? text)
  61. {
  62. if (string.IsNullOrWhiteSpace(text))
  63. return true;
  64. foreach (var value in TextSearchValues())
  65. {
  66. if (value.Contains(text, StringComparison.InvariantCultureIgnoreCase))
  67. return true;
  68. }
  69. return false;
  70. }
  71. #region Row Get/Set Caching
  72. // We do this for three reasons:
  73. // 1. Rather than define properties in once class and columns in another,
  74. // we can define and link properties and columns in the one class,
  75. // using a _static_ constructor, which reduces complexity
  76. // 2. By caching based on the property name, we eliminate the need to convert
  77. // expressions to strings (expensive), while still retaining type-safety
  78. // 3. Using the Get/Set helper functions reduces code complexity when defining
  79. // shell properties, and distinguishes between data and calculated properties
  80. private static ShellColumns<TParent, TEntity> _columns;
  81. public ShellColumns<TParent, TEntity> Columns
  82. {
  83. get
  84. {
  85. if (_columns == null)
  86. {
  87. _columns = new ShellColumns<TParent, TEntity>();
  88. _columns.Map(nameof(ID), x => x.ID);
  89. ConfigureColumns(_columns);
  90. }
  91. return _columns;
  92. }
  93. }
  94. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  95. protected virtual T Get<T>([CallerMemberName] string property = null)
  96. {
  97. if (_entity != null)
  98. {
  99. return (T)CoreUtils.GetPropertyValue(
  100. _entity,
  101. CoreUtils.GetFullPropertyName(Columns[property], ".")
  102. );
  103. }
  104. if (_row != null)
  105. {
  106. var col = _columns?.IndexOf(property);
  107. if (col != null)
  108. return Row.Get<T>(col.Value); //() Row.Values[];
  109. }
  110. return CoreUtils.GetDefault<T>();
  111. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  112. }
  113. protected virtual void Set<T>(T value, bool notify = true, [CallerMemberName] string property = null)
  114. {
  115. CheckEntity();
  116. CoreUtils.SetPropertyValue(
  117. _entity,
  118. CoreUtils.GetFullPropertyName(Columns[property], "."),
  119. value
  120. );
  121. //Row.Values[Columns.IndexOf(property)] = value;
  122. if (notify)
  123. DoPropertyChanged(property);
  124. }
  125. #endregion
  126. }
  127. }