forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAppSettings.cs
More file actions
47 lines (35 loc) · 1.53 KB
/
Copy pathSimpleAppSettings.cs
File metadata and controls
47 lines (35 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 textValue = value is string s
? s
: 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 List<KeyValuePair<string, string>> GetKeyValuePairs(string key) => GetString(key).FromJsv<List<KeyValuePair<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;
}
}
}