| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ModificationTrackingBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.Specialized; |
| | | 10 | | using System.ComponentModel; |
| | | 11 | | using System.Reflection; |
| | | 12 | | using System.Threading; |
| | | 13 | | using MyNet.Metadata; |
| | | 14 | | using MyNet.Observable.Behaviors.Metadata.Features; |
| | | 15 | | using MyNet.Reflection; |
| | | 16 | | |
| | | 17 | | namespace MyNet.Observable.Behaviors; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Provides a behavior that tracks modifications to an object and its nested properties, including collections. It impl |
| | | 21 | | /// </summary> |
| | | 22 | | public sealed class ModificationTrackingBehavior : SuspendableBehavior<ObservableObject>, IModificationTrackingBehavior |
| | | 23 | | { |
| | | 24 | | private readonly CollectionTrackingBehavior? _collections; |
| | | 25 | | private readonly bool _ownsCollections; |
| | 63 | 26 | | private readonly HashSet<string> _attachedProperties = new(StringComparer.Ordinal); |
| | 63 | 27 | | private readonly HashSet<IModificationAware> _trackedChildren = []; |
| | 63 | 28 | | private readonly Dictionary<IModificationAware, INotifyPropertyChanged> _childNotifiers = []; |
| | 63 | 29 | | private readonly Lock _attachSync = new(); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="ModificationTrackingBehavior"/> class with the specified owner. Thi |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="owner">The owner object.</param> |
| | | 35 | | public ModificationTrackingBehavior(ObservableObject owner) |
| | 63 | 36 | | : this(owner, new(owner), ownsCollections: true) |
| | | 37 | | { |
| | 63 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="ModificationTrackingBehavior"/> class. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="owner">The owner object.</param> |
| | | 44 | | /// <param name="collections">The collection tracking behavior.</param> |
| | | 45 | | public ModificationTrackingBehavior(ObservableObject owner, CollectionTrackingBehavior collections) |
| | 0 | 46 | | : this(owner, collections, ownsCollections: false) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | private ModificationTrackingBehavior(ObservableObject owner, CollectionTrackingBehavior? collections, bool ownsColle |
| | 63 | 51 | | : base(owner) |
| | | 52 | | { |
| | 63 | 53 | | _collections = collections; |
| | 63 | 54 | | _ownsCollections = ownsCollections; |
| | | 55 | | |
| | 63 | 56 | | _collections?.CollectionChanged += OnCollectionChanged; |
| | | 57 | | |
| | 63 | 58 | | EnsureInitialStateAttached(); |
| | 63 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public bool IsModified |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 144 | 66 | | EnsureInitialStateAttached(); |
| | 144 | 67 | | return field; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private set; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Determines whether the specified property should be tracked for modifications, using metadata from <see cref="Me |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="propertyName">The name of the property to check.</param> |
| | | 77 | | /// <returns><c>true</c> if the property should be tracked; otherwise, <c>false</c>.</returns> |
| | 5739 | 78 | | private bool ShouldTrack(string propertyName) => !MetadataRegistry.Get(Owner.GetType()).GetProperty(propertyName).Tr |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Marks the object as modified. |
| | | 82 | | /// </summary> |
| | | 83 | | public void MarkAsModified() |
| | | 84 | | { |
| | 114 | 85 | | if (IsDisposed || IsSuspended) |
| | 0 | 86 | | return; |
| | | 87 | | |
| | 114 | 88 | | if (IsModified) |
| | 69 | 89 | | return; |
| | | 90 | | |
| | 45 | 91 | | IsModified = true; |
| | | 92 | | |
| | 45 | 93 | | RaiseIsModifiedChanged(); |
| | 45 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Resets the modification state. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="recursive"> |
| | | 100 | | /// Indicates whether nested objects should also be reset. |
| | | 101 | | /// </param> |
| | | 102 | | public void ResetModified(bool recursive = true) |
| | | 103 | | { |
| | 6 | 104 | | EnsureInitialStateAttached(); |
| | | 105 | | |
| | 6 | 106 | | if (recursive) |
| | | 107 | | { |
| | 18 | 108 | | foreach (var child in _trackedChildren) |
| | 3 | 109 | | child.ResetModified(); |
| | | 110 | | } |
| | | 111 | | |
| | 6 | 112 | | if (!IsModified) |
| | 0 | 113 | | return; |
| | | 114 | | |
| | 6 | 115 | | IsModified = false; |
| | | 116 | | |
| | 6 | 117 | | RaiseIsModifiedChanged(); |
| | 6 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | 0 | 121 | | void IModificationAware.ResetModified() => ResetModified(); |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public void OnPropertyChanged(PropertyMutationContext context) |
| | | 125 | | { |
| | 189 | 126 | | EnsureInitialStateAttached(); |
| | | 127 | | |
| | 189 | 128 | | if (IsDisposed || IsSuspended) |
| | 0 | 129 | | return; |
| | | 130 | | |
| | 189 | 131 | | if (string.IsNullOrWhiteSpace(context.PropertyName)) |
| | 0 | 132 | | return; |
| | | 133 | | |
| | 189 | 134 | | if (!ShouldTrack(context.PropertyName)) |
| | 3 | 135 | | return; |
| | | 136 | | |
| | 186 | 137 | | if (Equals(context.OldValue, context.NewValue)) |
| | 81 | 138 | | return; |
| | | 139 | | |
| | 105 | 140 | | Detach(context.OldValue); |
| | 105 | 141 | | Attach(context.NewValue); |
| | | 142 | | |
| | 105 | 143 | | MarkAsModified(); |
| | 105 | 144 | | } |
| | | 145 | | |
| | | 146 | | #region Collections (via CollectionTrackingBehavior) |
| | | 147 | | |
| | | 148 | | private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| | | 149 | | { |
| | 0 | 150 | | EnsureInitialStateAttached(); |
| | | 151 | | |
| | 0 | 152 | | if (IsDisposed || IsSuspended) |
| | 0 | 153 | | return; |
| | | 154 | | |
| | 0 | 155 | | MarkAsModified(); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | #endregion |
| | | 159 | | |
| | | 160 | | #region Attach / Detach |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Attaches to readable owner properties that are already initialized. Properties whose getters fail (for example w |
| | | 164 | | /// </summary> |
| | | 165 | | internal void EnsureInitialStateAttached() |
| | | 166 | | { |
| | 402 | 167 | | if (IsDisposed) |
| | 0 | 168 | | return; |
| | | 169 | | |
| | | 170 | | lock (_attachSync) |
| | | 171 | | { |
| | 11904 | 172 | | foreach (var property in Owner.GetType().GetPublicProperties()) |
| | 5550 | 173 | | TryAttachProperty(property); |
| | 402 | 174 | | } |
| | 402 | 175 | | } |
| | | 176 | | |
| | | 177 | | private static bool MayYieldModificationAwareValue(Type propertyType) |
| | | 178 | | { |
| | 72 | 179 | | propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType; |
| | | 180 | | |
| | 72 | 181 | | return typeof(IModificationAware).IsAssignableFrom(propertyType) || (typeof(System.Collections.IEnumerable).IsAs |
| | 72 | 182 | | && propertyType != typeof(string)); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private void TryAttachProperty(PropertyInfo property) |
| | | 186 | | { |
| | 5550 | 187 | | if (!ShouldTrack(property.Name) || _attachedProperties.Contains(property.Name)) |
| | 4587 | 188 | | return; |
| | | 189 | | |
| | 963 | 190 | | if (!PropertyValueAccess.TryGetValue(Owner, property, out var value)) |
| | 96 | 191 | | return; |
| | | 192 | | |
| | 867 | 193 | | if (value is null && MayYieldModificationAwareValue(property.PropertyType)) |
| | 39 | 194 | | return; |
| | | 195 | | |
| | 828 | 196 | | _attachedProperties.Add(property.Name); |
| | 828 | 197 | | Attach(value); |
| | 828 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Attaches to a value if it is modification-aware or a collection. This method is called when a property changes t |
| | | 202 | | /// </summary> |
| | | 203 | | private void Attach(object? value) |
| | | 204 | | { |
| | 933 | 205 | | if (value is IModificationAware child) |
| | | 206 | | { |
| | 12 | 207 | | if (_trackedChildren.Add(child)) |
| | | 208 | | { |
| | 9 | 209 | | if (value is INotifyPropertyChanged npc) |
| | | 210 | | { |
| | 9 | 211 | | _childNotifiers[child] = npc; |
| | 9 | 212 | | npc.PropertyChanged += OnChildChanged; |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | } |
| | 933 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Detaches from a value if it is modification-aware or a collection. This method is called when a property changes |
| | | 220 | | /// </summary> |
| | | 221 | | private void Detach(object? value) |
| | | 222 | | { |
| | 105 | 223 | | if (value is IModificationAware child) |
| | | 224 | | { |
| | 0 | 225 | | if (_trackedChildren.Remove(child)) |
| | | 226 | | { |
| | 0 | 227 | | if (_childNotifiers.TryGetValue(child, out var npc)) |
| | | 228 | | { |
| | 0 | 229 | | npc.PropertyChanged -= OnChildChanged; |
| | 0 | 230 | | _childNotifiers.Remove(child); |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | } |
| | 105 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Handles the PropertyChanged event of a child object that is modification-aware. This method is called when a chi |
| | | 238 | | /// </summary> |
| | | 239 | | /// <param name="sender">The child object that raised the event.</param> |
| | | 240 | | /// <param name="e">The event data.</param> |
| | | 241 | | private void OnChildChanged(object? sender, PropertyChangedEventArgs e) |
| | | 242 | | { |
| | 9 | 243 | | if (IsDisposed || IsSuspended) |
| | 0 | 244 | | return; |
| | | 245 | | |
| | 9 | 246 | | if (sender is IModificationAware { IsModified: true }) |
| | 9 | 247 | | MarkAsModified(); |
| | 9 | 248 | | } |
| | | 249 | | |
| | | 250 | | #endregion |
| | | 251 | | |
| | | 252 | | #region Notifications |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Raises a notification that the IsModified property has changed. This method is called whenever the modification |
| | | 256 | | /// </summary> |
| | 51 | 257 | | private void RaiseIsModifiedChanged() => Owner.NotifyPropertyChanged(nameof(IModificationAware.IsModified)); |
| | | 258 | | |
| | | 259 | | #endregion |
| | | 260 | | |
| | | 261 | | #region Dispose |
| | | 262 | | |
| | | 263 | | /// <inheritdoc /> |
| | | 264 | | protected override void DisposeManagedResources() |
| | | 265 | | { |
| | 3 | 266 | | _collections?.CollectionChanged -= OnCollectionChanged; |
| | | 267 | | |
| | 3 | 268 | | if (_ownsCollections) |
| | 3 | 269 | | _collections?.Dispose(); |
| | | 270 | | |
| | 6 | 271 | | foreach (var notifier in _childNotifiers.Values) |
| | 0 | 272 | | notifier.PropertyChanged -= OnChildChanged; |
| | | 273 | | |
| | 3 | 274 | | _childNotifiers.Clear(); |
| | 3 | 275 | | _trackedChildren.Clear(); |
| | 3 | 276 | | _attachedProperties.Clear(); |
| | | 277 | | |
| | 3 | 278 | | base.DisposeManagedResources(); |
| | 3 | 279 | | } |
| | | 280 | | |
| | | 281 | | #endregion |
| | | 282 | | } |
| | | 283 | | |