| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ObservableBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Observable.Behaviors; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a base behavior attached to an <see cref="IObservableObject"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="owner">The owner object.</param> |
| | | 15 | | /// <exception cref="ArgumentNullException">Thrown when owner is null.</exception> |
| | | 16 | | public abstract class ObservableBehavior<T>(T owner) : IObservableBehavior, IDisposable |
| | | 17 | | where T : IObservableObject |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the owner object. |
| | | 21 | | /// </summary> |
| | 444 | 22 | | protected T Owner { get; } = owner ?? throw new ArgumentNullException(nameof(owner)); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets a value indicating whether the behavior is disposed. |
| | | 26 | | /// </summary> |
| | | 27 | | protected bool IsDisposed { get; private set; } |
| | | 28 | | |
| | | 29 | | #region IDisposable Support |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 33 | | /// </summary> |
| | | 34 | | protected virtual void DisposeManagedResources() |
| | | 35 | | { |
| | 33 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Disposes the object and releases all resources. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="disposing">Indicates whether the method is called from Dispose (true) or from a finalizer (false).< |
| | | 42 | | protected virtual void Dispose(bool disposing) |
| | | 43 | | { |
| | 33 | 44 | | if (IsDisposed) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | 33 | 47 | | if (disposing) |
| | 33 | 48 | | DisposeManagedResources(); |
| | | 49 | | |
| | 33 | 50 | | IsDisposed = true; |
| | 33 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Disposes the object and releases all resources. |
| | | 55 | | /// </summary> |
| | | 56 | | public void Dispose() |
| | | 57 | | { |
| | 33 | 58 | | Dispose(true); |
| | 33 | 59 | | GC.SuppressFinalize(this); |
| | 33 | 60 | | } |
| | | 61 | | |
| | | 62 | | #endregion IDisposable Support |
| | | 63 | | } |
| | | 64 | | |