From a7e88efc22dc2d8ce863e37228ba7783a0533c13 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 28 May 2020 06:46:31 +0200 Subject: [PATCH] Improve dictionary dashboard performance significantly --- .../Editors/DictionaryController.cs | 51 +++++++------------ 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Umbraco.Web/Editors/DictionaryController.cs b/src/Umbraco.Web/Editors/DictionaryController.cs index 06bb4cbbf6..f3246276b1 100644 --- a/src/Umbraco.Web/Editors/DictionaryController.cs +++ b/src/Umbraco.Web/Editors/DictionaryController.cs @@ -267,44 +267,31 @@ namespace Umbraco.Web.Editors /// public IEnumerable GetList() { - var list = new List(); + var items = Services.LocalizationService.GetDictionaryItemDescendants(null).ToArray(); + var list = new List(items.Length); - const int level = 0; - - foreach (var dictionaryItem in Services.LocalizationService.GetRootDictionaryItems().OrderBy(ItemSort())) + // recursive method to build a tree structure from the flat structure returned above + void BuildTree(int level = 0, Guid? parentId = null) { - var item = Mapper.Map(dictionaryItem); - item.Level = 0; - list.Add(item); + var children = items.Where(t => t.ParentId == parentId).ToArray(); + if(children.Any() == false) + { + return; + } - GetChildItemsForList(dictionaryItem, level + 1, list); + foreach(var child in children.OrderBy(ItemSort())) + { + var display = Mapper.Map(child); + display.Level = level; + list.Add(display); + + BuildTree(level + 1, child.Key); + } } - return list; - } + BuildTree(); - /// - /// Get child items for list. - /// - /// - /// The dictionary item. - /// - /// - /// The level. - /// - /// - /// The list. - /// - private void GetChildItemsForList(IDictionaryItem dictionaryItem, int level, ICollection list) - { - foreach (var childItem in Services.LocalizationService.GetDictionaryItemChildren(dictionaryItem.Key).OrderBy(ItemSort())) - { - var item = Mapper.Map(childItem); - item.Level = level; - list.Add(item); - - GetChildItemsForList(childItem, level + 1, list); - } + return list; } private static Func ItemSort() => item => item.ItemKey;