diff --git a/src/Umbraco.Abstractions/Models/IPropertyType.cs b/src/Umbraco.Abstractions/Models/IPropertyType.cs index 4447a858ab..e70ee39933 100644 --- a/src/Umbraco.Abstractions/Models/IPropertyType.cs +++ b/src/Umbraco.Abstractions/Models/IPropertyType.cs @@ -4,40 +4,84 @@ using Umbraco.Core.Models.Entities; namespace Umbraco.Core.Models { - public interface IPropertyType : IEntity + public interface IPropertyType : IEntity, IRememberBeingDirty { - [DataMember] + /// + /// Gets of sets the name of the property type. + /// string Name { get; } - [DataMember] + + /// + /// Gets of sets the alias of the property type. + /// string Alias { get; } - [DataMember] + + /// + /// Gets of sets the description of the property type. + /// string Description { get; } - [DataMember] + + /// + /// Gets or sets the identifier of the datatype for this property type. + /// int DataTypeId { get; } - [DataMember] + Guid DataTypeKey { get; } - [DataMember] + + /// + /// Gets or sets the alias of the property editor for this property type. + /// string PropertyEditorAlias { get; } - [DataMember] + + /// + /// Gets or sets the database type for storing value for this property type. + /// ValueStorageType ValueStorageType { get; } - [DataMember] + + /// + /// Gets or sets the identifier of the property group this property type belongs to. + /// + /// For generic properties, the value is null. Lazy PropertyGroupId { get; } - [DataMember] + + /// + /// Gets of sets a value indicating whether a value for this property type is required. + /// bool Mandatory { get; } - [DataMember] + + /// + /// Gets of sets the sort order of the property type. + /// int SortOrder { get; } - [DataMember] + + /// + /// Gets or sets the regular expression validating the property values. + /// string ValidationRegExp { get; } bool SupportsPublishing { get; } + /// + /// Gets or sets the content variation of the property type. + /// ContentVariation Variations { get; } - - + /// + /// Determines whether the property type supports a combination of culture and segment. + /// + /// The culture. + /// The segment. + /// A value indicating whether wildcards are valid. bool SupportsVariation(string culture, string segment, bool wildcards = false); + + /// + /// Converts a value assigned to a property. + /// + /// + /// The input value can be pretty much anything, and is converted to the actual CLR type + /// expected by the property (eg an integer if the property values are integers). + /// Throws if the value cannot be converted. + /// object ConvertAssignedValue(object value); - - } } diff --git a/src/Umbraco.Core/Models/PropertyType.cs b/src/Umbraco.Core/Models/PropertyType.cs index 1448e099be..3cbe1ab70b 100644 --- a/src/Umbraco.Core/Models/PropertyType.cs +++ b/src/Umbraco.Core/Models/PropertyType.cs @@ -36,7 +36,7 @@ namespace Umbraco.Core.Models { if (dataType == null) throw new ArgumentNullException(nameof(dataType)); - if(dataType.HasIdentity) + if (dataType.HasIdentity) _dataTypeId = dataType.Id; _propertyEditorAlias = dataType.EditorAlias; @@ -58,14 +58,16 @@ namespace Umbraco.Core.Models /// public PropertyType(string propertyEditorAlias, ValueStorageType valueStorageType) : this(propertyEditorAlias, valueStorageType, false) - { } + { + } /// /// Initializes a new instance of the class. /// public PropertyType(string propertyEditorAlias, ValueStorageType valueStorageType, string propertyTypeAlias) : this(propertyEditorAlias, valueStorageType, false, propertyTypeAlias) - { } + { + } /// /// Initializes a new instance of the class. @@ -99,9 +101,7 @@ namespace Umbraco.Core.Models /// public bool SupportsPublishing { get; internal set; } - /// - /// Gets of sets the name of the property type. - /// + /// [DataMember] public string Name { @@ -109,9 +109,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _name, nameof(Name)); } - /// - /// Gets of sets the alias of the property type. - /// + /// [DataMember] public virtual string Alias { @@ -119,9 +117,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(SanitizeAlias(value), ref _alias, nameof(Alias)); } - /// - /// Gets of sets the description of the property type. - /// + /// [DataMember] public string Description { @@ -129,9 +125,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _description, nameof(Description)); } - /// - /// Gets or sets the identifier of the datatype for this property type. - /// + /// [DataMember] public int DataTypeId { @@ -146,9 +140,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _dataTypeKey, nameof(DataTypeKey)); } - /// - /// Gets or sets the alias of the property editor for this property type. - /// + /// [DataMember] public string PropertyEditorAlias { @@ -156,9 +148,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _propertyEditorAlias, nameof(PropertyEditorAlias)); } - /// - /// Gets or sets the database type for storing value for this property type. - /// + /// [DataMember] public ValueStorageType ValueStorageType { @@ -170,10 +160,7 @@ namespace Umbraco.Core.Models } } - /// - /// Gets or sets the identifier of the property group this property type belongs to. - /// - /// For generic properties, the value is null. + /// [DataMember] public Lazy PropertyGroupId { @@ -181,9 +168,8 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _propertyGroupId, nameof(PropertyGroupId)); } - /// - /// Gets of sets a value indicating whether a value for this property type is required. - /// + + /// [DataMember] public bool Mandatory { @@ -191,9 +177,8 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _mandatory, nameof(Mandatory)); } - /// - /// Gets of sets the sort order of the property type. - /// + + /// [DataMember] public int SortOrder { @@ -201,9 +186,7 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _sortOrder, nameof(SortOrder)); } - /// - /// Gets or sets the regular expression validating the property values. - /// + /// [DataMember] public string ValidationRegExp { @@ -211,21 +194,14 @@ namespace Umbraco.Core.Models set => SetPropertyValueAndDetectChanges(value, ref _validationRegExp, nameof(ValidationRegExp)); } - /// - /// Gets or sets the content variation of the property type. - /// + /// public ContentVariation Variations { get => _variations; set => SetPropertyValueAndDetectChanges(value, ref _variations, nameof(Variations)); } - /// - /// Determines whether the property type supports a combination of culture and segment. - /// - /// The culture. - /// The segment. - /// A value indicating whether wildcards are valid. + /// public bool SupportsVariation(string culture, string segment, bool wildcards = false) { // exact validation: cannot accept a 'null' culture if the property type varies @@ -249,7 +225,7 @@ namespace Umbraco.Core.Models /// If the value is of the expected type, it can be directly assigned to the property. /// Otherwise, some conversion is required. /// - public bool IsOfExpectedPropertyType(object value) + private bool IsOfExpectedPropertyType(object value) { // null values are assumed to be ok if (value == null) @@ -275,19 +251,7 @@ namespace Umbraco.Core.Models } } - /// - /// Determines whether a value can be assigned to a property. - /// - public bool IsValueAssignable(object value) => TryConvertAssignedValue(value, false, out _); - - /// - /// Converts a value assigned to a property. - /// - /// - /// The input value can be pretty much anything, and is converted to the actual CLR type - /// expected by the property (eg an integer if the property values are integers). - /// Throws if the value cannot be converted. - /// + /// public object ConvertAssignedValue(object value) => TryConvertAssignedValue(value, true, out var converted) ? converted : null; /// @@ -296,8 +260,6 @@ namespace Umbraco.Core.Models /// /// /// - public bool TryConvertAssignedValue(object value, out object converted) => TryConvertAssignedValue(value, false, out converted); - private bool TryConvertAssignedValue(object value, bool throwOnError, out object converted) { var isOfExpectedType = IsOfExpectedPropertyType(value); @@ -332,6 +294,7 @@ namespace Umbraco.Core.Models converted = convInt.Result; return true; } + if (throwOnError) ThrowTypeException(value, typeof(int), Alias); return false; @@ -347,6 +310,7 @@ namespace Umbraco.Core.Models converted = convDecimal.Result.Normalize(); return true; } + if (throwOnError) ThrowTypeException(value, typeof(decimal), Alias); return false; @@ -360,6 +324,7 @@ namespace Umbraco.Core.Models converted = convDateTime.Result; return true; } + if (throwOnError) ThrowTypeException(value, typeof(DateTime), Alias); return false; @@ -413,7 +378,7 @@ namespace Umbraco.Core.Models { base.PerformDeepClone(clone); - var clonedEntity = (PropertyType)clone; + var clonedEntity = (PropertyType) clone; //need to manually assign the Lazy value as it will not be automatically mapped if (PropertyGroupId != null)