From 8cbe53f49db33dc1c640f4dfad7a160bcddc399b Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 8 Apr 2015 11:30:35 +1000 Subject: [PATCH] Updates PropertyType to have a ctor overload accepting an alias when one already exists. Updates it's equality comparisons to: check the base comparison, then check for a case insensitive comparison with alias, fixes GetHashCode to be consistent with the Equals method. --- src/Umbraco.Core/Models/PropertyType.cs | 62 ++++++++++++++++++------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Core/Models/PropertyType.cs b/src/Umbraco.Core/Models/PropertyType.cs index 5bf96d2578..4aab35c805 100644 --- a/src/Umbraco.Core/Models/PropertyType.cs +++ b/src/Umbraco.Core/Models/PropertyType.cs @@ -38,11 +38,21 @@ namespace Umbraco.Core.Models _propertyEditorAlias = dataTypeDefinition.PropertyEditorAlias; _dataTypeDatabaseType = dataTypeDefinition.DatabaseType; } + + public PropertyType(IDataTypeDefinition dataTypeDefinition, string propertyTypeAlias) + : this(dataTypeDefinition) + { + SetAlias(propertyTypeAlias); + } - internal PropertyType(string propertyEditorAlias, DataTypeDatabaseType dataTypeDatabaseType) + public PropertyType(string propertyEditorAlias, DataTypeDatabaseType dataTypeDatabaseType) : this(propertyEditorAlias, dataTypeDatabaseType, false) { - PropertyEditorAlias = propertyEditorAlias; + } + + public PropertyType(string propertyEditorAlias, DataTypeDatabaseType dataTypeDatabaseType, string propertyTypeAlias) + : this(propertyEditorAlias, dataTypeDatabaseType, false, propertyTypeAlias) + { } /// @@ -58,6 +68,21 @@ namespace Umbraco.Core.Models _dataTypeDatabaseType = dataTypeDatabaseType; } + /// + /// Used internally to assign an explicity database type for this property type regardless of what the underlying data type/property editor is. + /// + /// + /// + /// + /// + internal PropertyType(string propertyEditorAlias, DataTypeDatabaseType dataTypeDatabaseType, bool isExplicitDbType, string propertyTypeAlias) + { + _isExplicitDbType = isExplicitDbType; + _propertyEditorAlias = propertyEditorAlias; + _dataTypeDatabaseType = dataTypeDatabaseType; + SetAlias(propertyTypeAlias); + } + private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); private static readonly PropertyInfo AliasSelector = ExpressionHelper.GetPropertyInfo(x => x.Alias); private static readonly PropertyInfo DescriptionSelector = ExpressionHelper.GetPropertyInfo(x => x.Description); @@ -96,15 +121,9 @@ namespace Umbraco.Core.Models get { return _alias; } set { - //NOTE: WE are doing this because we don't want to do a ToSafeAlias when the alias is the special case of - // being prefixed with Constants.PropertyEditors.InternalGenericPropertiesPrefix - // which is used internally - SetPropertyValueAndDetectChanges(o => { - _alias = value.StartsWith(Constants.PropertyEditors.InternalGenericPropertiesPrefix) - ? value - : value.ToCleanString(CleanStringType.Alias | CleanStringType.UmbracoCase); + SetAlias(value); return _alias; }, _alias, AliasSelector); } @@ -284,6 +303,17 @@ namespace Umbraco.Core.Models } } + private void SetAlias(string value) + { + //NOTE: WE are doing this because we don't want to do a ToSafeAlias when the alias is the special case of + // being prefixed with Constants.PropertyEditors.InternalGenericPropertiesPrefix + // which is used internally + + _alias = value.StartsWith(Constants.PropertyEditors.InternalGenericPropertiesPrefix) + ? value + : value.ToCleanString(CleanStringType.Alias | CleanStringType.UmbracoCase); + } + /// /// Create a new Property object from a "raw" database value. /// @@ -431,26 +461,22 @@ namespace Umbraco.Core.Models public bool Equals(PropertyType other) { - //Check whether the compared object is null. - if (ReferenceEquals(other, null)) return false; - - //Check whether the compared object references the same data. - if (ReferenceEquals(this, other)) return true; + if (base.Equals(other)) return true; //Check whether the PropertyType's properties are equal. - return Alias.Equals(other.Alias) && Name.Equals(other.Name); + return Alias.InvariantEquals(other.Alias); } public override int GetHashCode() { //Get hash code for the Name field if it is not null. - int hashName = Name == null ? 0 : Name.GetHashCode(); + int baseHash = base.GetHashCode(); //Get hash code for the Alias field. - int hashAlias = Alias.GetHashCode(); + int hashAlias = Alias.ToLowerInvariant().GetHashCode(); //Calculate the hash code for the product. - return hashName ^ hashAlias; + return baseHash ^ hashAlias; } public override object DeepClone()