Shell.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  11. where TParent : IModel
  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. new Client<TEntity>().Save(_entity, auditmessage);
  42. }
  43. public void Cancel()
  44. {
  45. _entity?.CancelChanges();
  46. _entity = null;
  47. }
  48. private CoreRow _row;
  49. public CoreRow Row
  50. {
  51. get => _row;
  52. set
  53. {
  54. _row = value;
  55. RowChanged();
  56. }
  57. }
  58. public TParent Parent { get; set; }
  59. IModel IShell.Parent => this.Parent;
  60. public Guid ID => Get<Guid>();
  61. #region Row Get/Set Caching
  62. // We do this for three reasons:
  63. // 1. Rather than define properties in once class and columns in another,
  64. // we can define and link properties and columns in the one class,
  65. // using a _static_ constructor, which reduces complexity
  66. // 2. By caching based on the property name, we eliminate the need to convert
  67. // expressions to strings (expensive), while still retaining type-safety
  68. // 3. Using the Get/Set helper functions reduces code complexity when defining
  69. // shell properties, and distinguishes between data and calculated properties
  70. private static ShellColumns<TParent, TEntity> _columns;
  71. public ShellColumns<TParent, TEntity> Columns
  72. {
  73. get
  74. {
  75. if (_columns == null)
  76. {
  77. _columns = new ShellColumns<TParent, TEntity>();
  78. _columns.Map(nameof(ID), x => x.ID);
  79. ConfigureColumns(_columns);
  80. }
  81. return _columns;
  82. }
  83. }
  84. protected abstract void ConfigureColumns(ShellColumns<TParent, TEntity> columns);
  85. protected virtual T Get<T>([CallerMemberName] string property = null)
  86. {
  87. if (_entity != null)
  88. {
  89. return (T)CoreUtils.GetPropertyValue(
  90. _entity,
  91. CoreUtils.GetFullPropertyName(Columns[property], ".")
  92. );
  93. }
  94. var col = _columns.IndexOf(property);
  95. var value = Row.Get<T>(col); //() Row.Values[];
  96. return value;
  97. //return value != null ? (T)CoreUtils.ChangeType(value, typeof(T)) : CoreUtils.GetDefault<T>();
  98. }
  99. protected virtual void Set<T>(T value, bool notify = true, [CallerMemberName] string property = null)
  100. {
  101. CheckEntity();
  102. CoreUtils.SetPropertyValue(
  103. _entity,
  104. CoreUtils.GetFullPropertyName(Columns[property], "."),
  105. value
  106. );
  107. //Row.Values[Columns.IndexOf(property)] = value;
  108. if (notify)
  109. DoPropertyChanged(property);
  110. }
  111. #endregion
  112. }
  113. }