Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KayeeNL/47b3b904df63b91451e3e7a33db9d965 to your computer and use it in GitHub Desktop.
Save KayeeNL/47b3b904df63b91451e3e7a33db9d965 to your computer and use it in GitHub Desktop.
kayee-blogpost-sitecore-dictionary-service-dictionarymodel.cs
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