From 995b31b86c2dd203ae8cd3d5ba5dd910a3820f55 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 13 Nov 2019 12:17:09 +1100 Subject: [PATCH] Cleans up IProperty --- .../Collections/DeepCloneableList.cs | 16 +++-- .../Models/Entities/EntitySlim.cs | 10 +++ .../Models/Entities/ICanBeDirty.cs | 10 +++ src/Umbraco.Abstractions/Models/IProperty.cs | 63 +------------------ src/Umbraco.Core/Models/SimpleContentType.cs | 3 + src/Umbraco.Tests/Models/Collections/Item.cs | 52 +++++++-------- 6 files changed, 63 insertions(+), 91 deletions(-) diff --git a/src/Umbraco.Abstractions/Collections/DeepCloneableList.cs b/src/Umbraco.Abstractions/Collections/DeepCloneableList.cs index a21c7aae56..cc57b96f1b 100644 --- a/src/Umbraco.Abstractions/Collections/DeepCloneableList.cs +++ b/src/Umbraco.Abstractions/Collections/DeepCloneableList.cs @@ -47,8 +47,7 @@ namespace Umbraco.Core.Collections var newList = new DeepCloneableList(ListCloneBehavior.None); foreach (var item in this) { - var dc = item as IDeepCloneable; - if (dc != null) + if (item is IDeepCloneable dc) { newList.Add((T)dc.DeepClone()); } @@ -66,8 +65,7 @@ namespace Umbraco.Core.Collections var newList2 = new DeepCloneableList(ListCloneBehavior.Always); foreach (var item in this) { - var dc = item as IDeepCloneable; - if (dc != null) + if (item is IDeepCloneable dc) { newList2.Add((T)dc.DeepClone()); } @@ -121,6 +119,16 @@ namespace Umbraco.Core.Collections } } + public void DisableChangeTracking() + { + // noop + } + + public void EnableChangeTracking() + { + // noop + } + public void ResetWereDirtyProperties() { foreach (var dc in this.OfType()) diff --git a/src/Umbraco.Abstractions/Models/Entities/EntitySlim.cs b/src/Umbraco.Abstractions/Models/Entities/EntitySlim.cs index b095965056..77611ebc3d 100644 --- a/src/Umbraco.Abstractions/Models/Entities/EntitySlim.cs +++ b/src/Umbraco.Abstractions/Models/Entities/EntitySlim.cs @@ -146,6 +146,16 @@ namespace Umbraco.Core.Models.Entities throw new WontImplementException(); } + public void DisableChangeTracking() + { + // noop + } + + public void EnableChangeTracking() + { + // noop + } + public bool WasDirty() { throw new WontImplementException(); diff --git a/src/Umbraco.Abstractions/Models/Entities/ICanBeDirty.cs b/src/Umbraco.Abstractions/Models/Entities/ICanBeDirty.cs index fc95161d7e..57a8a581f0 100644 --- a/src/Umbraco.Abstractions/Models/Entities/ICanBeDirty.cs +++ b/src/Umbraco.Abstractions/Models/Entities/ICanBeDirty.cs @@ -26,5 +26,15 @@ namespace Umbraco.Core.Models.Entities /// Resets dirty properties. /// void ResetDirtyProperties(); + + /// + /// Disables change tracking. + /// + void DisableChangeTracking(); + + /// + /// Enables change tracking. + /// + void EnableChangeTracking(); } } diff --git a/src/Umbraco.Abstractions/Models/IProperty.cs b/src/Umbraco.Abstractions/Models/IProperty.cs index cd8a07393e..35a151af10 100644 --- a/src/Umbraco.Abstractions/Models/IProperty.cs +++ b/src/Umbraco.Abstractions/Models/IProperty.cs @@ -4,7 +4,7 @@ using Umbraco.Core.Models.Entities; namespace Umbraco.Core.Models { - public interface IProperty + public interface IProperty : IEntity, IRememberBeingDirty { ValueStorageType ValueStorageType { get; } @@ -23,24 +23,6 @@ namespace Umbraco.Core.Models /// string Alias { get; } - /// - int Id { get; set; } - - /// - Guid Key { get; set; } - - /// - DateTime CreateDate { get; set; } - - /// - DateTime UpdateDate { get; set; } - - /// - DateTime? DeleteDate { get; set; } // no change tracking - not persisted - - /// - bool HasIdentity { get; } - /// /// Gets the value. /// @@ -56,49 +38,6 @@ namespace Umbraco.Core.Models /// void ResetIdentity(); - bool Equals(EntityBase other); - bool Equals(object obj); - int GetHashCode(); - object DeepClone(); - - /// - bool IsDirty(); - - /// - bool IsPropertyDirty(string propertyName); - - /// - IEnumerable GetDirtyProperties(); - - /// - /// Saves dirty properties so they can be checked with WasDirty. - void ResetDirtyProperties(); - - /// - bool WasDirty(); - - /// - bool WasPropertyDirty(string propertyName); - - /// - void ResetWereDirtyProperties(); - - /// - void ResetDirtyProperties(bool rememberDirty); - - /// - IEnumerable GetWereDirtyProperties(); - - /// - /// Disables change tracking. - /// - void DisableChangeTracking(); - - /// - /// Enables change tracking. - /// - void EnableChangeTracking(); - int PropertyTypeId { get; } void PublishValues(string culture = "*", string segment = "*"); void UnpublishValues(string culture = "*", string segment = "*"); diff --git a/src/Umbraco.Core/Models/SimpleContentType.cs b/src/Umbraco.Core/Models/SimpleContentType.cs index 5c81017ec8..fc84d15529 100644 --- a/src/Umbraco.Core/Models/SimpleContentType.cs +++ b/src/Umbraco.Core/Models/SimpleContentType.cs @@ -105,6 +105,7 @@ namespace Umbraco.Core.Models // we have to have all this, because we're an IUmbracoEntity, because that is // required by the query expression visitor / SimpleContentTypeMapper + // TODO: Make the query expression visitor use a different common interface, or investigate removing IRememberBeingDirty from being a requirement on that interface and expliclty checking for that throughout the code? string ITreeEntity.Name { get => this.Name; set => throw new NotImplementedException(); } int IEntity.Id { get => this.Id; set => throw new NotImplementedException(); } @@ -130,5 +131,7 @@ namespace Umbraco.Core.Models bool ICanBeDirty.IsPropertyDirty(string propName) => throw new NotImplementedException(); IEnumerable ICanBeDirty.GetDirtyProperties() => throw new NotImplementedException(); void ICanBeDirty.ResetDirtyProperties() => throw new NotImplementedException(); + public void DisableChangeTracking() => throw new NotImplementedException(); + public void EnableChangeTracking() => throw new NotImplementedException(); } } diff --git a/src/Umbraco.Tests/Models/Collections/Item.cs b/src/Umbraco.Tests/Models/Collections/Item.cs index 5d1ab24fb0..7c7f207aa2 100644 --- a/src/Umbraco.Tests/Models/Collections/Item.cs +++ b/src/Umbraco.Tests/Models/Collections/Item.cs @@ -11,6 +11,7 @@ namespace Umbraco.Tests.Models.Collections { public abstract class Item : IEntity, ICanBeDirty { + private bool _withChanges = true; // should we track changes? private bool _hasIdentity; private int _id; private Guid _key; @@ -26,10 +27,7 @@ namespace Umbraco.Tests.Models.Collections [DataMember] public int Id { - get - { - return _id; - } + get => _id; set { _id = value; @@ -45,14 +43,8 @@ namespace Umbraco.Tests.Models.Collections [DataMember] public Guid Key { - get - { - if (_key == Guid.Empty) - return _id.ToGuid(); - - return _key; - } - set { _key = value; } + get => _key == Guid.Empty ? _id.ToGuid() : _key; + set => _key = value; } /// @@ -93,12 +85,12 @@ namespace Umbraco.Tests.Models.Collections /// The property info. protected virtual void OnPropertyChanged(PropertyInfo propertyInfo) { + if (_withChanges == false) + return; + _propertyChangedInfo[propertyInfo.Name] = true; - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo.Name)); - } + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyInfo.Name)); } internal virtual void ResetIdentity() @@ -128,7 +120,7 @@ namespace Umbraco.Tests.Models.Collections /// Tracks the properties that have changed /// //private readonly IDictionary _propertyChangedInfo = new Dictionary(); - private IDictionary _propertyChangedInfo; + private readonly IDictionary _propertyChangedInfo; /// /// Indicates whether a specific property on the current entity is dirty. @@ -166,19 +158,29 @@ namespace Umbraco.Tests.Models.Collections _propertyChangedInfo.Clear(); } + /// + /// Disables change tracking. + /// + public void DisableChangeTracking() + { + _withChanges = false; + } + + /// + /// Enables change tracking. + /// + public void EnableChangeTracking() + { + _withChanges = true; + } + /// /// Indicates whether the current entity has an identity, eg. Id. /// public virtual bool HasIdentity { - get - { - return _hasIdentity; - } - protected set - { - _hasIdentity = value; - } + get => _hasIdentity; + protected set => _hasIdentity = value; } public static bool operator ==(Item left, Item right)