public void Initialize() { using var db = dataBaseFactory.CreateDb(); var categories = db.Categories.Where(x => x.DeletedDate == null).Select(x => new CategoryCached(x)) .ToDictionary(x => x.Id); foreach (var category in categories.Values) { category.Init1_ParentAndSub(categories); } RootCategory = categories.Values.FirstOrDefault(x => x.Name == Category.RootCategoryName); if (RootCategory == null) { throw new Exception($"Can not find category '{Category.RootCategoryName}' in data base."); } var categoriesList = RootCategory.Init2_AllSub(); categoriesList.Insert(0, RootCategory); RootCategory.Init3_UrlPaths(); RootCategory.Init4_InitSectionsRoots(); foreach (var category in categoriesList) { category.Init5_SetListsAndFreeze(); } AllCategoriesByName = categoriesList.ToImmutableDictionary(x => x.NameNormalized, StringComparer.OrdinalIgnoreCase); AllCategoriesById = AllCategoriesByName.ToImmutableDictionary(x => x.Value.Id, x => x.Value); }
public CategoryCached GetCategory(string name) { return(AllCategoriesByName.TryGetValue(name, out CategoryCached categoryCached) ? categoryCached : null); }