DataType refactoring preparation - Entity refactoring
This commit is contained in:
@@ -7,8 +7,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Web;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Core.Models.Entities;
|
||||
|
||||
namespace Umbraco.Core.Models
|
||||
{
|
||||
@@ -18,20 +17,13 @@ namespace Umbraco.Core.Models
|
||||
[Serializable]
|
||||
[DataContract(IsReference = true)]
|
||||
[DebuggerDisplay("Id: {Id}, Name: {Name}, ContentType: {ContentTypeBase.Alias}")]
|
||||
public abstract class ContentBase : EntityBase.EntityBase, IContentBase
|
||||
public abstract class ContentBase : TreeEntityBase, IContentBase
|
||||
{
|
||||
private static readonly Lazy<PropertySelectors> Ps = new Lazy<PropertySelectors>();
|
||||
|
||||
private IDictionary<string, object> _additionalData;
|
||||
private int _contentTypeId;
|
||||
protected IContentTypeComposition ContentTypeBase;
|
||||
|
||||
private Lazy<int> _parentId;
|
||||
private int _level;
|
||||
private string _path;
|
||||
private int _sortOrder;
|
||||
|
||||
private bool _trashed;
|
||||
private int _creatorId;
|
||||
private int _writerId;
|
||||
|
||||
// fixme need to deal with localized names, how?
|
||||
@@ -40,10 +32,6 @@ namespace Umbraco.Core.Models
|
||||
|
||||
private PropertyCollection _properties;
|
||||
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
IDictionary<string, object> IUmbracoEntity.AdditionalData => _lazyAdditionalData.Value;
|
||||
private readonly Lazy<Dictionary<string, object>> _lazyAdditionalData = new Lazy<Dictionary<string, object>>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ContentBase"/> class.
|
||||
/// </summary>
|
||||
@@ -51,7 +39,7 @@ namespace Umbraco.Core.Models
|
||||
: this(name, contentType, properties)
|
||||
{
|
||||
if (parentId == 0) throw new ArgumentOutOfRangeException(nameof(parentId));
|
||||
_parentId = new Lazy<int>(() => parentId);
|
||||
ParentId = parentId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +49,7 @@ namespace Umbraco.Core.Models
|
||||
: this(name, contentType, properties)
|
||||
{
|
||||
if (parent == null) throw new ArgumentNullException(nameof(parent));
|
||||
_parentId = new Lazy<int>(() => parent.Id);
|
||||
SetParent(parent);
|
||||
}
|
||||
|
||||
private ContentBase(string name, IContentTypeComposition contentType, PropertyCollection properties)
|
||||
@@ -72,7 +60,7 @@ namespace Umbraco.Core.Models
|
||||
Id = 0; // no identity
|
||||
VersionId = 0; // no versions
|
||||
|
||||
_name = name;
|
||||
Name = _name = name;
|
||||
_contentTypeId = contentType.Id;
|
||||
_properties = properties ?? throw new ArgumentNullException(nameof(properties));
|
||||
_properties.EnsurePropertyTypes(PropertyTypes);
|
||||
@@ -81,116 +69,27 @@ namespace Umbraco.Core.Models
|
||||
// ReSharper disable once ClassNeverInstantiated.Local
|
||||
private class PropertySelectors
|
||||
{
|
||||
public readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.ParentId);
|
||||
public readonly PropertyInfo LevelSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.Level);
|
||||
public readonly PropertyInfo PathSelector = ExpressionHelper.GetPropertyInfo<ContentBase, string>(x => x.Path);
|
||||
public readonly PropertyInfo SortOrderSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.SortOrder);
|
||||
public readonly PropertyInfo TrashedSelector = ExpressionHelper.GetPropertyInfo<ContentBase, bool>(x => x.Trashed);
|
||||
public readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo<ContentBase, string>(x => x.Name);
|
||||
public readonly PropertyInfo CreatorIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.CreatorId);
|
||||
public readonly PropertyInfo DefaultContentTypeIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.ContentTypeId);
|
||||
public readonly PropertyInfo PropertyCollectionSelector = ExpressionHelper.GetPropertyInfo<ContentBase, PropertyCollection>(x => x.Properties);
|
||||
public readonly PropertyInfo WriterSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.WriterId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[DataMember]
|
||||
[DoNotClone]
|
||||
IDictionary<string, object> IUmbracoEntity.AdditionalData => _additionalData ?? (_additionalData = new Dictionary<string, object>());
|
||||
|
||||
/// <inheritdoc />
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[IgnoreDataMember]
|
||||
bool IUmbracoEntity.HasAdditionalData => _additionalData != null;
|
||||
|
||||
protected void PropertiesChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
OnPropertyChanged(Ps.Value.PropertyCollectionSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the identifier of the parent entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual int ParentId
|
||||
{
|
||||
get
|
||||
{
|
||||
var val = _parentId.Value;
|
||||
if (val == 0)
|
||||
{
|
||||
throw new InvalidOperationException("The ParentId cannot have a value of 0. Perhaps the parent object used to instantiate this object has not been persisted to the data store.");
|
||||
}
|
||||
return val;
|
||||
}
|
||||
set
|
||||
{
|
||||
_parentId = new Lazy<int>(() => value);
|
||||
OnPropertyChanged(Ps.Value.ParentIdSelector);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the identifier of the parent entity.
|
||||
/// </summary>
|
||||
/// <param name="parentId">Id of the Parent</param>
|
||||
protected internal void SetLazyParentId(Lazy<int> parentId)
|
||||
{
|
||||
_parentId = parentId;
|
||||
OnPropertyChanged(Ps.Value.ParentIdSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the level of the entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual int Level
|
||||
{
|
||||
get => _level;
|
||||
set => SetPropertyValueAndDetectChanges(value, ref _level, Ps.Value.LevelSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the path of the entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual string Path //Setting this value should be handled by the class not the user
|
||||
{
|
||||
get => _path;
|
||||
set => SetPropertyValueAndDetectChanges(value, ref _path, Ps.Value.PathSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the sort order of the entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual int SortOrder
|
||||
{
|
||||
get => _sortOrder;
|
||||
set => SetPropertyValueAndDetectChanges(value, ref _sortOrder, Ps.Value.SortOrderSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the entity is trashed.
|
||||
/// </summary>
|
||||
/// <remarks>A trashed entity is unpublished and in the recycle bin.</remarks>
|
||||
[DataMember]
|
||||
public virtual bool Trashed // fixme setting this value should be handled by the class not the user
|
||||
{
|
||||
get => _trashed;
|
||||
internal set => SetPropertyValueAndDetectChanges(value, ref _trashed, Ps.Value.TrashedSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual string Name
|
||||
{
|
||||
get => _name;
|
||||
set => SetPropertyValueAndDetectChanges(value, ref _name, Ps.Value.NameSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the identifier of the user who created the entity.
|
||||
/// </summary>
|
||||
[DataMember]
|
||||
public virtual int CreatorId
|
||||
{
|
||||
get => _creatorId;
|
||||
set => SetPropertyValueAndDetectChanges(value, ref _creatorId, Ps.Value.CreatorIdSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Id of the user who wrote/updated this entity
|
||||
/// </summary>
|
||||
@@ -220,7 +119,7 @@ namespace Umbraco.Core.Models
|
||||
}
|
||||
return _contentTypeId;
|
||||
}
|
||||
protected set { SetPropertyValueAndDetectChanges(value, ref _contentTypeId, Ps.Value.DefaultContentTypeIdSelector); }
|
||||
protected set => SetPropertyValueAndDetectChanges(value, ref _contentTypeId, Ps.Value.DefaultContentTypeIdSelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user