Initial commit of refactor of better/more efficient in terms of mem and performance properpty selectors for our models, this also yields a much cleaner syntax. I have discovered one issue now which is that the PropertyChanged event now fires before the property value is actually set, this is why the lambda syntax existed before. Instead we can use a ref property for the SetPropertyValueAndDetectChanges methods which will work much nicer - and also potentially have less allocations.
This commit is contained in:
@@ -23,13 +23,17 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
private DateTime _updateDate;
|
||||
private bool _wasCancelled;
|
||||
|
||||
private static readonly PropertyInfo IdSelector = ExpressionHelper.GetPropertyInfo<Entity, int>(x => x.Id);
|
||||
private static readonly PropertyInfo KeySelector = ExpressionHelper.GetPropertyInfo<Entity, Guid>(x => x.Key);
|
||||
private static readonly PropertyInfo CreateDateSelector = ExpressionHelper.GetPropertyInfo<Entity, DateTime>(x => x.CreateDate);
|
||||
private static readonly PropertyInfo UpdateDateSelector = ExpressionHelper.GetPropertyInfo<Entity, DateTime>(x => x.UpdateDate);
|
||||
private static readonly PropertyInfo HasIdentitySelector = ExpressionHelper.GetPropertyInfo<Entity, bool>(x => x.HasIdentity);
|
||||
private static readonly PropertyInfo WasCancelledSelector = ExpressionHelper.GetPropertyInfo<Entity, bool>(x => x.WasCancelled);
|
||||
|
||||
private static readonly Lazy<PropertySelectors> Ps = new Lazy<PropertySelectors>();
|
||||
|
||||
private class PropertySelectors
|
||||
{
|
||||
public readonly PropertyInfo IdSelector = ExpressionHelper.GetPropertyInfo<Entity, int>(x => x.Id);
|
||||
public readonly PropertyInfo KeySelector = ExpressionHelper.GetPropertyInfo<Entity, Guid>(x => x.Key);
|
||||
public readonly PropertyInfo CreateDateSelector = ExpressionHelper.GetPropertyInfo<Entity, DateTime>(x => x.CreateDate);
|
||||
public readonly PropertyInfo UpdateDateSelector = ExpressionHelper.GetPropertyInfo<Entity, DateTime>(x => x.UpdateDate);
|
||||
public readonly PropertyInfo HasIdentitySelector = ExpressionHelper.GetPropertyInfo<Entity, bool>(x => x.HasIdentity);
|
||||
public readonly PropertyInfo WasCancelledSelector = ExpressionHelper.GetPropertyInfo<Entity, bool>(x => x.WasCancelled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Integer Id
|
||||
@@ -37,18 +41,11 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
[DataMember]
|
||||
public int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
get { return _id; }
|
||||
set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_id = value;
|
||||
HasIdentity = true; //set the has Identity
|
||||
return _id;
|
||||
}, _id, IdSelector);
|
||||
_id = SetPropertyValueAndDetectChanges(value, _id, Ps.Value.IdSelector);
|
||||
HasIdentity = true; //set the has Identity
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,14 +64,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
_key = Guid.NewGuid();
|
||||
return _key;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_key = value;
|
||||
return _key;
|
||||
}, _key, KeySelector);
|
||||
}
|
||||
set { _key = SetPropertyValueAndDetectChanges(value, _key, Ps.Value.KeySelector); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -84,14 +74,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
public DateTime CreateDate
|
||||
{
|
||||
get { return _createDate; }
|
||||
set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_createDate = value;
|
||||
return _createDate;
|
||||
}, _createDate, CreateDateSelector);
|
||||
}
|
||||
set { _createDate = SetPropertyValueAndDetectChanges(value, _createDate, Ps.Value.CreateDateSelector); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -105,14 +88,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
internal bool WasCancelled
|
||||
{
|
||||
get { return _wasCancelled; }
|
||||
set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_wasCancelled = value;
|
||||
return _wasCancelled;
|
||||
}, _wasCancelled, WasCancelledSelector);
|
||||
}
|
||||
set { _wasCancelled = SetPropertyValueAndDetectChanges(value, _wasCancelled, Ps.Value.WasCancelledSelector); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -122,14 +98,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
public DateTime UpdateDate
|
||||
{
|
||||
get { return _updateDate; }
|
||||
set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_updateDate = value;
|
||||
return _updateDate;
|
||||
}, _updateDate, UpdateDateSelector);
|
||||
}
|
||||
set { _updateDate = SetPropertyValueAndDetectChanges(value, _updateDate, Ps.Value.UpdateDateSelector); }
|
||||
}
|
||||
|
||||
internal virtual void ResetIdentity()
|
||||
@@ -166,14 +135,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
{
|
||||
return _hasIdentity;
|
||||
}
|
||||
protected set
|
||||
{
|
||||
SetPropertyValueAndDetectChanges(o =>
|
||||
{
|
||||
_hasIdentity = value;
|
||||
return _hasIdentity;
|
||||
}, _hasIdentity, HasIdentitySelector);
|
||||
}
|
||||
protected set { _hasIdentity = SetPropertyValueAndDetectChanges(value, _hasIdentity, Ps.Value.HasIdentitySelector); }
|
||||
}
|
||||
|
||||
//TODO: Make this NOT virtual or even exist really!
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
/// <returns>True if Property was changed, otherwise False. Returns false if the entity had not been previously changed.</returns>
|
||||
public virtual bool WasPropertyDirty(string propertyName)
|
||||
{
|
||||
return WasDirty() && _lastPropertyChangedInfo.Any(x => x.Key == propertyName);
|
||||
return _lastPropertyChangedInfo != null && _lastPropertyChangedInfo.Any(x => x.Key == propertyName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -133,7 +133,10 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
if (rememberPreviouslyChangedProperties)
|
||||
{
|
||||
//copy the changed properties to the last changed properties
|
||||
_lastPropertyChangedInfo = _propertyChangedInfo.ToDictionary(v => v.Key, v => v.Value);
|
||||
if (_propertyChangedInfo != null)
|
||||
{
|
||||
_lastPropertyChangedInfo = _propertyChangedInfo.ToDictionary(v => v.Key, v => v.Value);
|
||||
}
|
||||
}
|
||||
|
||||
//NOTE: We cannot .Clear() because when we memberwise clone this will be the SAME
|
||||
@@ -161,8 +164,8 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
/// Used by inheritors to set the value of properties, this will detect if the property value actually changed and if it did
|
||||
/// it will ensure that the property has a dirty flag set.
|
||||
/// </summary>
|
||||
/// <param name="setValue"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="newVal"></param>
|
||||
/// <param name="origVal"></param>
|
||||
/// <param name="propertySelector"></param>
|
||||
/// <returns>returns true if the value changed</returns>
|
||||
/// <remarks>
|
||||
@@ -170,26 +173,22 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
/// save a document type, nearly all properties are flagged as dirty just because we've 'reset' them, but they are all set
|
||||
/// to the same value, so it's really not dirty.
|
||||
/// </remarks>
|
||||
internal bool SetPropertyValueAndDetectChanges<T>(Func<T, T> setValue, T value, PropertyInfo propertySelector)
|
||||
internal T SetPropertyValueAndDetectChanges<T>(T newVal, T origVal, PropertyInfo propertySelector)
|
||||
{
|
||||
if ((typeof(T) == typeof(string) == false) && TypeHelper.IsTypeAssignableFrom<IEnumerable>(typeof(T)))
|
||||
{
|
||||
throw new InvalidOperationException("This method does not support IEnumerable instances. For IEnumerable instances a manual custom equality check will be required");
|
||||
}
|
||||
|
||||
return SetPropertyValueAndDetectChanges(setValue, value, propertySelector,
|
||||
new DelegateEqualityComparer<T>(
|
||||
//Standard Equals comparison
|
||||
(arg1, arg2) => Equals(arg1, arg2),
|
||||
arg => arg.GetHashCode()));
|
||||
return SetPropertyValueAndDetectChanges(newVal, origVal, propertySelector, EqualityComparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used by inheritors to set the value of properties, this will detect if the property value actually changed and if it did
|
||||
/// it will ensure that the property has a dirty flag set.
|
||||
/// </summary>
|
||||
/// <param name="setValue"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="newVal"></param>
|
||||
/// <param name="origVal"></param>
|
||||
/// <param name="propertySelector"></param>
|
||||
/// <param name="comparer">The equality comparer to use</param>
|
||||
/// <returns>returns true if the value changed</returns>
|
||||
@@ -198,18 +197,17 @@ namespace Umbraco.Core.Models.EntityBase
|
||||
/// save a document type, nearly all properties are flagged as dirty just because we've 'reset' them, but they are all set
|
||||
/// to the same value, so it's really not dirty.
|
||||
/// </remarks>
|
||||
internal bool SetPropertyValueAndDetectChanges<T>(Func<T, T> setValue, T value, PropertyInfo propertySelector, IEqualityComparer<T> comparer)
|
||||
internal T SetPropertyValueAndDetectChanges<T>(T newVal, T origVal, PropertyInfo propertySelector, IEqualityComparer<T> comparer)
|
||||
{
|
||||
var initVal = value;
|
||||
var newVal = setValue(value);
|
||||
//don't track changes, just return the value
|
||||
if (_changeTrackingEnabled == false) return newVal;
|
||||
|
||||
//don't track changes, just set the value (above)
|
||||
if (_changeTrackingEnabled == false) return false;
|
||||
if (comparer.Equals(origVal, newVal) == false)
|
||||
{
|
||||
OnPropertyChanged(propertySelector);
|
||||
}
|
||||
|
||||
if (comparer.Equals(initVal, newVal)) return false;
|
||||
|
||||
OnPropertyChanged(propertySelector);
|
||||
return true;
|
||||
return newVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user