Permalink
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (35 sloc) 1.44 KB
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Configuration;
namespace ServiceStack
{
public class SimpleAppSettings : IAppSettings
{
private readonly Dictionary<string, string> settings;
public SimpleAppSettings(Dictionary<string, string> settings = null) =>
this.settings = settings ?? new Dictionary<string, string>();
public Dictionary<string, string> GetAll() => settings;
public List<string> GetAllKeys() => settings.Keys.ToList();
public bool Exists(string key) => settings.ContainsKey(key);
public void Set<T>(string key, T value)
{
var s = value as string;
var textValue = s != null
? (string)(object)value
: value.ToJsv();
settings[key] = textValue;
}
public string GetString(string key) => settings.TryGetValue(key, out string value)
? value
: null;
public IList<string> GetList(string key) => GetString(key).FromJsv<List<string>>();
public IDictionary<string, string> GetDictionary(string key) => GetString(key).FromJsv<Dictionary<string, string>>();
public T Get<T>(string key) => GetString(key).FromJsv<T>();
public T Get<T>(string key, T defaultValue)
{
var value = GetString(key);
return value != null ? value.FromJsv<T>() : defaultValue;
}
}
}