diff --git a/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs b/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs index 8e4fbc13f5..109084e9b9 100644 --- a/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs +++ b/src/Umbraco.Core/Models/EntityBase/TracksChangesEntityBase.cs @@ -18,7 +18,9 @@ namespace Umbraco.Core.Models.EntityBase //TODO: This needs to go on to ICanBeDirty http://issues.umbraco.org/issue/U4-5662 public virtual IEnumerable GetDirtyProperties() { - return _propertyChangedInfo.Where(x => x.Value).Select(x => x.Key); + return _propertyChangedInfo == null + ? Enumerable.Empty() + : _propertyChangedInfo.Where(x => x.Value).Select(x => x.Key); } private bool _changeTrackingEnabled = true; @@ -26,12 +28,12 @@ namespace Umbraco.Core.Models.EntityBase /// /// Tracks the properties that have changed /// - private IDictionary _propertyChangedInfo = new Dictionary(); + private IDictionary _propertyChangedInfo; /// /// Tracks the properties that we're changed before the last commit (or last call to ResetDirtyProperties) /// - private IDictionary _lastPropertyChangedInfo = null; + private IDictionary _lastPropertyChangedInfo; /// /// Property changed event @@ -47,12 +49,12 @@ namespace Umbraco.Core.Models.EntityBase //return if we're not tracking changes if (_changeTrackingEnabled == false) return; + if (_propertyChangedInfo == null) + _propertyChangedInfo = new Dictionary(); + _propertyChangedInfo[propertyInfo.Name] = true; - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo.Name)); - } + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyInfo.Name)); } /// @@ -62,7 +64,7 @@ namespace Umbraco.Core.Models.EntityBase /// True if Property is dirty, otherwise False public virtual bool IsPropertyDirty(string propertyName) { - return _propertyChangedInfo.Any(x => x.Key == propertyName); + return _propertyChangedInfo != null && _propertyChangedInfo.Any(x => x.Key == propertyName); } /// @@ -71,7 +73,7 @@ namespace Umbraco.Core.Models.EntityBase /// True if entity is dirty, otherwise False public virtual bool IsDirty() { - return _propertyChangedInfo.Any(); + return _propertyChangedInfo != null && _propertyChangedInfo.Any(); } /// @@ -100,7 +102,7 @@ namespace Umbraco.Core.Models.EntityBase { //NOTE: We cannot .Clear() because when we memberwise clone this will be the SAME // instance as the one on the clone, so we need to create a new instance. - _lastPropertyChangedInfo = new Dictionary(); + _lastPropertyChangedInfo = null; } /// @@ -135,13 +137,13 @@ namespace Umbraco.Core.Models.EntityBase //NOTE: We cannot .Clear() because when we memberwise clone this will be the SAME // instance as the one on the clone, so we need to create a new instance. - _propertyChangedInfo = new Dictionary(); + _propertyChangedInfo = null; } public void ResetChangeTrackingCollections() { - _propertyChangedInfo = new Dictionary(); - _lastPropertyChangedInfo = new Dictionary(); + _propertyChangedInfo = null; + _lastPropertyChangedInfo = null; } public void DisableChangeTracking() @@ -179,7 +181,6 @@ namespace Umbraco.Core.Models.EntityBase //Standard Equals comparison (arg1, arg2) => Equals(arg1, arg2), arg => arg.GetHashCode())); - } /// @@ -204,14 +205,10 @@ namespace Umbraco.Core.Models.EntityBase //don't track changes, just set the value (above) if (_changeTrackingEnabled == false) return false; - if (comparer.Equals(initVal, newVal) == false) - { - OnPropertyChanged(propertySelector); - return true; - } - return false; + if (comparer.Equals(initVal, newVal)) return false; + + OnPropertyChanged(propertySelector); + return true; } - - } } \ No newline at end of file