Shell.cs 4.4 KB

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