Esempio n. 1
0
        public async Task <T> GetAsync <T>(string key) where T : IConvertible
        {
            Config configuration = _cacheProvider.Get(key) ?? await _dataSource.GetAsync(_applicationName, key);

            if (configuration == null)
            {
                return(default(T));
            }

            var conversionType = typeof(T);

            if (configuration.Type != conversionType.Name)
            {
                throw new InvalidCastException($"Configuration type {configuration.Type} is different than expected type {conversionType.Name}");
            }

            var convertedValue = (T)Convert.ChangeType(configuration.Value, conversionType);

            _cacheProvider.Set(key, configuration);
            return(convertedValue);
        }