DataValidatiorBase.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Data;
  5. namespace Avalonia.Data.Core.Plugins
  6. {
  7. /// <summary>
  8. /// Base class for data validators.
  9. /// </summary>
  10. /// <remarks>
  11. /// Data validators are <see cref="IPropertyAccessor"/>s that are returned from an
  12. /// <see cref="IDataValidationPlugin"/>. They wrap an inner <see cref="IPropertyAccessor"/>
  13. /// and convert any values received from the inner property accessor into
  14. /// <see cref="BindingNotification"/>s.
  15. /// </remarks>
  16. public abstract class DataValidatiorBase : PropertyAccessorBase, IObserver<object>
  17. {
  18. private readonly IPropertyAccessor _inner;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="DataValidatiorBase"/> class.
  21. /// </summary>
  22. /// <param name="inner">The inner property accessor.</param>
  23. protected DataValidatiorBase(IPropertyAccessor inner)
  24. {
  25. _inner = inner;
  26. }
  27. /// <inheritdoc/>
  28. public override Type PropertyType => _inner.PropertyType;
  29. /// <inheritdoc/>
  30. public override object Value => _inner.Value;
  31. /// <inheritdoc/>
  32. public override bool SetValue(object value, BindingPriority priority) => _inner.SetValue(value, priority);
  33. /// <summary>
  34. /// Should never be called: the inner <see cref="IPropertyAccessor"/> should never notify
  35. /// completion.
  36. /// </summary>
  37. void IObserver<object>.OnCompleted() { }
  38. /// <summary>
  39. /// Should never be called: the inner <see cref="IPropertyAccessor"/> should never notify
  40. /// an error.
  41. /// </summary>
  42. void IObserver<object>.OnError(Exception error) { }
  43. /// <summary>
  44. /// Called when the inner <see cref="IPropertyAccessor"/> notifies with a new value.
  45. /// </summary>
  46. /// <param name="value">The value.</param>
  47. void IObserver<object>.OnNext(object value) => InnerValueChanged(value);
  48. /// <inheritdoc/>
  49. protected override void Dispose(bool disposing) => _inner.Dispose();
  50. /// <summary>
  51. /// Begins listening to the inner <see cref="IPropertyAccessor"/>.
  52. /// </summary>
  53. protected override void SubscribeCore(IObserver<object> observer) => _inner.Subscribe(this);
  54. /// <summary>
  55. /// Called when the inner <see cref="IPropertyAccessor"/> notifies with a new value.
  56. /// </summary>
  57. /// <param name="value">The value.</param>
  58. /// <remarks>
  59. /// Notifies the observer that the value has changed. The value will be wrapped in a
  60. /// <see cref="BindingNotification"/> if it is not already a binding notification.
  61. /// </remarks>
  62. protected virtual void InnerValueChanged(object value)
  63. {
  64. var notification = value as BindingNotification ?? new BindingNotification(value);
  65. Observer.OnNext(notification);
  66. }
  67. }
  68. }