-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathNetCoreAppSettings.cs
More file actions
159 lines (136 loc) · 4.84 KB
/
Copy pathNetCoreAppSettings.cs
File metadata and controls
159 lines (136 loc) · 4.84 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#if NETSTANDARD2_0
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Microsoft.Extensions.Configuration;
using ServiceStack.Configuration;
using ServiceStack.Text;
namespace ServiceStack
{
public class NetCoreAppSettings : IAppSettings
{
public IConfiguration Configuration { get; }
public NetCoreAppSettings(IConfiguration configuration) => Configuration = configuration;
static object GetValue(IConfigurationSection section)
{
if (section == null)
return null;
if (section.Value != null)
return section.Value;
var children = section.GetChildren();
var first = children.FirstOrDefault();
if (first == null)
return null;
if (first.Key == "0")
{
var to = children.Select(GetValue).ToList();
if (to.Count > 0)
return to;
}
else
{
var to = new Dictionary<string, object>();
foreach (var child in children)
{
to[child.Key] = GetValue(child);
}
if (to.Count > 0)
return to;
}
return null;
}
private static T Bind<T>(IConfigurationSection config)
{
try
{
if (config.Value != null)
return config.Value.ConvertTo<T>();
if (typeof(T).HasInterface(typeof(IEnumerable))
&& !typeof(T).HasInterface(typeof(IDictionary)))
{
var values = config.GetChildren().Map(GetValue);
return values.ConvertTo<T>();
}
}
catch (Exception ex)
{
var message = $"The {config.Key} setting had an invalid format. " +
$"The value \"{config.Value}\" could not be cast to type {typeof(T).FullName}";
throw new ConfigurationErrorsException(message, ex);
}
try
{
var to = typeof(T).CreateInstance();
config.Bind(to);
return (T)to;
}
catch (InvalidOperationException ex)
{
throw new ConfigurationErrorsException(ex.Message, ex);
}
}
private IConfigurationSection GetRequiredSection(string key)
{
var child = GetSection(key);
if (!child.Exists())
throw new ConfigurationErrorsException(string.Format(ErrorMessages.AppsettingNotFound, key));
return child;
}
private IConfigurationSection GetSection(string key)
{
var child = Configuration.GetChildren().FirstOrDefault(x => x.Key == key);
if (!child.Exists())
child = Configuration.GetSection(key);
return child;
}
public Dictionary<string, string> GetAll()
{
var to = new Dictionary<string, string>();
foreach (var kvp in Configuration.GetChildren())
{
to[kvp.Key] = kvp.Value;
}
return to;
}
public List<string> GetAllKeys() => Configuration.GetChildren()
.Map(x => x.Key);
public bool Exists(string key) => Configuration.GetChildren().Any(x => x.Key == key)
|| Configuration.GetSection(key).Exists();
public void Set<T>(string key, T value) => Configuration[key] = value is string
? value.ToString()
: TypeSerializer.SerializeToString(value);
public string GetString(string name) => Configuration[name];
public IList<string> GetList(string key)
{
var section = GetRequiredSection(key);
var members = section.GetChildren();
var to = members.Map(x => x.Value);
return to;
}
public IDictionary<string, string> GetDictionary(string key)
{
var to = Bind<Dictionary<string,string>>(GetRequiredSection(key));
return to;
}
public List<KeyValuePair<string, string>> GetKeyValuePairs(string key)
{
var section = GetRequiredSection(key);
return Bind<Dictionary<string,string>>(section).ToList();
}
public T Get<T>(string name)
{
return Get<T>(name, default);
}
public T Get<T>(string name, T defaultValue)
{
var child = GetSection(name);
if (!child.Exists())
return defaultValue;
var to = Bind<T>(child);
return to;
}
}
}
#endif