Created
January 4, 2024 12:12
-
-
Save KayeeNL/47b3b904df63b91451e3e7a33db9d965 to your computer and use it in GitHub Desktop.
kayee-blogpost-sitecore-dictionary-service-dictionarymodel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace KayeeDictionaryServiceDemo.Services.Dictionary.Models | |
{ | |
public class DictionaryModel | |
{ | |
private readonly Lazy<Task<Dictionary<string, string>>> _dictionaryDataTask; | |
public DictionaryModel(IServiceProvider serviceProvider) | |
{ | |
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | |
_dictionaryDataTask = new Lazy<Task<Dictionary<string, string>>>(FetchDictionaryDataAsync); | |
} | |
protected IServiceProvider ServiceProvider { get; } | |
private Dictionary<string, string> DictionaryData => _dictionaryDataTask.Value.Result; | |
private async Task<Dictionary<string, string>> FetchDictionaryDataAsync() | |
{ | |
var dictionaryService = ServiceProvider.GetService(typeof(IDictionaryService)) as IDictionaryService; | |
return await dictionaryService?.FetchDictionaryData() ?? new Dictionary<string, string>(); | |
} | |
public string GetDictionaryPhrase(string dictionaryKey, string defaultValue) | |
{ | |
return DictionaryData.GetValueOrDefault(dictionaryKey, defaultValue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment