From cf1069e0e1866e497404880a7ae831288122410d Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 5 Apr 2018 17:16:34 +0100 Subject: [PATCH] NestedContentPropertyEditor - code tidy-up A little tidy-up of these class method. I noticed we were calling `ToString()` twice in places, and the `ConfigureForDisplay` prevalues are already available as a Dictionary, so there's no need for an additional `ToDictionary` allocation. --- .../NestedContentPropertyEditor.cs | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs index 30acbafd0b..c229948677 100644 --- a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs @@ -2,9 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; -using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Umbraco.Core; @@ -104,10 +102,9 @@ namespace Umbraco.Web.PropertyEditors { base.ConfigureForDisplay(preValues); - var asDictionary = preValues.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value); - if (asDictionary.ContainsKey("hideLabel")) + if (preValues.PreValuesAsDictionary.ContainsKey("hideLabel")) { - var boolAttempt = asDictionary["hideLabel"].TryConvertTo(); + var boolAttempt = preValues.PreValuesAsDictionary["hideLabel"].Value.TryConvertTo(); if (boolAttempt.Success) { HideLabel = boolAttempt.Result; @@ -120,10 +117,14 @@ namespace Umbraco.Web.PropertyEditors public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { // Convert / validate value - if (property.Value == null || string.IsNullOrWhiteSpace(property.Value.ToString())) + if (property.Value == null) return string.Empty; - var value = JsonConvert.DeserializeObject>(property.Value.ToString()); + var propertyValue = property.Value.ToString(); + if (string.IsNullOrWhiteSpace(propertyValue)) + return string.Empty; + + var value = JsonConvert.DeserializeObject>(propertyValue); if (value == null) return string.Empty; @@ -192,10 +193,14 @@ namespace Umbraco.Web.PropertyEditors public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { - if (property.Value == null || string.IsNullOrWhiteSpace(property.Value.ToString())) + if (property.Value == null) return string.Empty; - var value = JsonConvert.DeserializeObject>(property.Value.ToString()); + var propertyValue = property.Value.ToString(); + if (string.IsNullOrWhiteSpace(propertyValue)) + return string.Empty; + + var value = JsonConvert.DeserializeObject>(propertyValue); if (value == null) return string.Empty; @@ -267,10 +272,14 @@ namespace Umbraco.Web.PropertyEditors public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue) { - if (editorValue.Value == null || string.IsNullOrWhiteSpace(editorValue.Value.ToString())) + if (editorValue.Value == null) return null; - var value = JsonConvert.DeserializeObject>(editorValue.Value.ToString()); + var rawValue = editorValue.Value.ToString(); + if (string.IsNullOrWhiteSpace(rawValue)) + return null; + + var value = JsonConvert.DeserializeObject>(rawValue); if (value == null) return null;