From 7d0d32b218c66b0474f5e6433b1d42f9469d361b Mon Sep 17 00:00:00 2001 From: mattbrailsford Date: Fri, 7 Feb 2014 16:11:58 +0000 Subject: [PATCH] Moved GetValue method body to the Property object to allow easy conversion of an untyped property without having to go via the fieldset again. --- .../Umbraco.Archetype/Models/Fieldset.cs | 60 +----------------- .../Umbraco.Archetype/Models/Property.cs | 63 ++++++++++++++++++- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs index b60b4eb..d3f0f90 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs @@ -1,9 +1,6 @@ using System.Collections.Generic; using System.Linq; using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.PropertyEditors; namespace Archetype.Umbraco.Models { @@ -29,52 +26,10 @@ namespace Archetype.Umbraco.Models { var property = GetProperty(propertyAlias); - if (property == null || string.IsNullOrEmpty(property.Value.ToString())) - return default(T); + if (property == null || string.IsNullOrEmpty(property.Value.ToString())) + return default(T); - var value = property.Value; - - // If the value is of type T, just return it - if (value is T) - return (T)value; - - // Umbraco has the concept of a IPropertyEditorValueConverter which it - // also queries for property resolvers. However I'm not sure what these - // are for, nor can I find any implementations in core, so am currently - // just ignoring these when looking up converters. - // NB: IPropertyEditorValueConverter not to be confused with - // IPropertyValueConverter which are the ones most people are creating - var properyType = CreateDummyPropertyType(property.DataTypeId, property.PropertyEditorAlias); - var converters = PropertyValueConvertersResolver.Current.Converters.ToArray(); - - // In umbraco, there are default value converters that try to convert the - // value if all else fails. The problem is, they are also in the list of - // converters, and the means for filtering these out is internal, so - // we currently have to try ALL converters to see if they can convert - // rather than just finding the most appropreate. If the ability to filter - // out default value converters becomes public, the following logic could - // and probably should be changed. - foreach (var converter in converters.Where(x => x.IsConverter(properyType))) - { - // Convert the type using a found value converter - var value2 = converter.ConvertDataToSource(properyType, value, false); - - // If the value is of type T, just return it - if (value2 is T) - return (T)value2; - - // Value is not final value type, so try a regular type conversion aswell - var convertAttempt = value2.TryConvertTo(); - if (convertAttempt.Success) - return convertAttempt.Result; - } - - // Value is not final value type, so try a regular type conversion - var convertAttempt2 = value.TryConvertTo(); - if (convertAttempt2.Success) - return convertAttempt2.Result; - - return default(T); + return property.GetValue(); } private Property GetProperty(string propertyAlias) @@ -82,15 +37,6 @@ namespace Archetype.Umbraco.Models return Properties.FirstOrDefault(p => p.Alias.InvariantEquals(propertyAlias)); } - private PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias) - { - return new PublishedPropertyType(null, - new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias) - { - Id = dataTypeId - })); - } - #endregion } diff --git a/app/Umbraco/Umbraco.Archetype/Models/Property.cs b/app/Umbraco/Umbraco.Archetype/Models/Property.cs index dc538c6..b6ba09f 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Property.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Property.cs @@ -1,10 +1,71 @@ -namespace Archetype.Umbraco.Models +using System.Linq; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; + +namespace Archetype.Umbraco.Models { public class Property { public string Alias { get; internal set; } public object Value { get; internal set; } public string PropertyEditorAlias { get; internal set; } + internal int DataTypeId { get; set; } + + public T GetValue() + { + // If the value is of type T, just return it + if (Value is T) + return (T)Value; + + // Umbraco has the concept of a IPropertyEditorValueConverter which it + // also queries for property resolvers. However I'm not sure what these + // are for, nor can I find any implementations in core, so am currently + // just ignoring these when looking up converters. + // NB: IPropertyEditorValueConverter not to be confused with + // IPropertyValueConverter which are the ones most people are creating + var properyType = CreateDummyPropertyType(DataTypeId, PropertyEditorAlias); + var converters = PropertyValueConvertersResolver.Current.Converters.ToArray(); + + // In umbraco, there are default value converters that try to convert the + // value if all else fails. The problem is, they are also in the list of + // converters, and the means for filtering these out is internal, so + // we currently have to try ALL converters to see if they can convert + // rather than just finding the most appropreate. If the ability to filter + // out default value converters becomes public, the following logic could + // and probably should be changed. + foreach (var converter in converters.Where(x => x.IsConverter(properyType))) + { + // Convert the type using a found value converter + var value2 = converter.ConvertDataToSource(properyType, Value, false); + + // If the value is of type T, just return it + if (value2 is T) + return (T)value2; + + // Value is not final value type, so try a regular type conversion aswell + var convertAttempt = value2.TryConvertTo(); + if (convertAttempt.Success) + return convertAttempt.Result; + } + + // Value is not final value type, so try a regular type conversion + var convertAttempt2 = Value.TryConvertTo(); + if (convertAttempt2.Success) + return convertAttempt2.Result; + + return default(T); + } + + private PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias) + { + return new PublishedPropertyType(null, + new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias) + { + Id = dataTypeId + })); + } } }