From 603b38bd6c3b1b7903c0c714d0a79fde9306402d Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Wed, 10 Feb 2021 12:47:35 +1300 Subject: [PATCH] implement lazy --- .../Implement/LocalizedTextService.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Core/Services/Implement/LocalizedTextService.cs b/src/Umbraco.Core/Services/Implement/LocalizedTextService.cs index 79c568e374..f5a85dda7e 100644 --- a/src/Umbraco.Core/Services/Implement/LocalizedTextService.cs +++ b/src/Umbraco.Core/Services/Implement/LocalizedTextService.cs @@ -15,8 +15,10 @@ namespace Umbraco.Core.Services.Implement { private readonly ILogger _logger; private readonly Lazy _fileSources; - private readonly IDictionary>> _dictionarySource; - private readonly IDictionary> _noAreaDictionarySource; + private IDictionary>> _dictionarySource => _dictionarySourceLazy.Value; + private IDictionary> _noAreaDictionarySource => _noAreaDictionarySourceLazy.Value; + private readonly Lazy>>> _dictionarySourceLazy; + private readonly Lazy>> _noAreaDictionarySourceLazy; private readonly char[] _splitter = new[] { '/' }; /// /// Initializes with a file sources instance @@ -28,9 +30,8 @@ namespace Umbraco.Core.Services.Implement if (logger == null) throw new ArgumentNullException("logger"); _logger = logger; if (fileSources == null) throw new ArgumentNullException("fileSources"); - var dictionaries = FileSourcesToDictionarySources(fileSources.Value); - _dictionarySource = dictionaries.WithArea; - _noAreaDictionarySource = dictionaries.WithoutArea; + _dictionarySourceLazy = new Lazy>>>(()=> FileSourcesToDictionarySources(fileSources.Value).WithArea); + _noAreaDictionarySourceLazy = new Lazy>>(() => FileSourcesToDictionarySources(fileSources.Value).WithoutArea); _fileSources = fileSources; } @@ -72,9 +73,9 @@ namespace Umbraco.Core.Services.Implement if (source == null) throw new ArgumentNullException("source"); if (logger == null) throw new ArgumentNullException("logger"); _logger = logger; - var dictionaries = XmlSourcesToDictionarySources(source); - _dictionarySource = dictionaries.WithArea; - _noAreaDictionarySource = dictionaries.WithoutArea; + _dictionarySourceLazy = new Lazy>>>(()=> XmlSourcesToDictionarySources(source).WithArea); + _noAreaDictionarySourceLazy = new Lazy>>(() => XmlSourcesToDictionarySources(source).WithoutArea); + } @@ -85,10 +86,11 @@ namespace Umbraco.Core.Services.Implement /// public LocalizedTextService(IDictionary>> source, ILogger logger) { - _dictionarySource = source ?? throw new ArgumentNullException(nameof(source)); + var dictionarySource = source ?? throw new ArgumentNullException(nameof(source)); + _dictionarySourceLazy = new Lazy>>>(() => dictionarySource); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); var cultureNoAreaDictionary = new Dictionary>(); - foreach (var cultureDictionary in _dictionarySource) + foreach (var cultureDictionary in dictionarySource) { var areaAliaValue = GetAreaStoredTranslations(source, cultureDictionary.Key); var aliasValue = new Dictionary(); @@ -104,7 +106,7 @@ namespace Umbraco.Core.Services.Implement } cultureNoAreaDictionary.Add(cultureDictionary.Key, aliasValue); } - _noAreaDictionarySource = cultureNoAreaDictionary; + _noAreaDictionarySourceLazy = new Lazy>>(() => cultureNoAreaDictionary); } public string Localize(string key, CultureInfo culture, IDictionary tokens = null)