From ce6a2175770edb071f0675f7d1c3b01a8cf55c78 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 24 Oct 2019 12:34:48 +0200 Subject: [PATCH] Performance boost for Nested Content: Don't load all content types all the time. --- .../NestedContentPropertyEditor.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs index 13dcc463f8..f511a97cac 100644 --- a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs @@ -40,14 +40,6 @@ namespace Umbraco.Web.PropertyEditors // has to be lazy else circular dep in ctor private PropertyEditorCollection PropertyEditors => _propertyEditors.Value; - private static IContentType GetElementType(JObject item) - { - var contentTypeAlias = item[ContentTypeAliasPropertyKey]?.ToObject(); - return string.IsNullOrEmpty(contentTypeAlias) - ? null - : Current.Services.ContentTypeService.Get(contentTypeAlias); - } - #region Pre Value Editor protected override IConfigurationEditor CreateConfigurationEditor() => new NestedContentConfigurationEditor(); @@ -62,11 +54,22 @@ namespace Umbraco.Web.PropertyEditors { private readonly PropertyEditorCollection _propertyEditors; + private readonly Lazy> _contentTypes = new Lazy>(() => + Current.Services.ContentTypeService.GetAll().ToDictionary(c => c.Alias) + ); + public NestedContentPropertyValueEditor(DataEditorAttribute attribute, PropertyEditorCollection propertyEditors) : base(attribute) { _propertyEditors = propertyEditors; - Validators.Add(new NestedContentValidator(propertyEditors)); + Validators.Add(new NestedContentValidator(propertyEditors, GetElementType)); + } + + + private IContentType GetElementType(JObject item) + { + var contentTypeAlias = item[ContentTypeAliasPropertyKey]?.ToObject() ?? string.Empty; + return _contentTypes.Value.ContainsKey(contentTypeAlias) ? _contentTypes.Value[contentTypeAlias] : null; } internal ServiceContext Services => Current.Services; @@ -285,10 +288,12 @@ namespace Umbraco.Web.PropertyEditors internal class NestedContentValidator : IValueValidator { private readonly PropertyEditorCollection _propertyEditors; + private readonly Func _getElementType; - public NestedContentValidator(PropertyEditorCollection propertyEditors) + public NestedContentValidator(PropertyEditorCollection propertyEditors, Func getElementType) { _propertyEditors = propertyEditors; + _getElementType = getElementType; } public IEnumerable Validate(object rawValue, string valueType, object dataTypeConfiguration) @@ -306,7 +311,7 @@ namespace Umbraco.Web.PropertyEditors var o = value[i]; var propValues = (JObject) o; - var contentType = GetElementType(propValues); + var contentType = _getElementType(propValues); if (contentType == null) continue; var propValueKeys = propValues.Properties().Select(x => x.Name).ToArray();