forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigUtils.cs
More file actions
206 lines (188 loc) · 6.26 KB
/
ConfigUtils.cs
File metadata and controls
206 lines (188 loc) · 6.26 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
namespace ServiceStack.Configuration
{
public class ConfigUtils
{
const int KeyIndex = 0;
const int ValueIndex = 1;
const string ErrorAppsettingNotFound = "Unable to find App Setting: {0}";
const string ErrorConnectionStringNotFound = "Unable to find Connection String: {0}";
const string ErrorCreatingType = "Error creating type {0} from text '{1}";
const char ItemSeperator = ',';
const char KeyValueSeperator = ':';
const string ConfigNullValue = "{null}";
/// <summary>
/// Gets the nullable app setting.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static string GetNullableAppSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
/// <summary>
/// Gets the app setting.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static string GetAppSetting(string key)
{
string value = ConfigurationManager.AppSettings[key];
if (value == null)
{
throw new ConfigurationErrorsException(String.Format(ErrorAppsettingNotFound, key));
}
return value;
}
/// <summary>
/// Determines wheter the Config section identified by the sectionName exists.
/// </summary>
/// <param name="sectionName">Name of the section.</param>
/// <returns></returns>
public static bool ConfigSectionExists(string sectionName)
{
return (ConfigurationManager.GetSection(sectionName) != null);
}
/// <summary>
/// Returns AppSetting[key] if exists otherwise defaultValue
/// </summary>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
public static string GetAppSetting(string key, string defaultValue)
{
return ConfigurationManager.AppSettings[key] ?? defaultValue;
}
/// <summary>
/// Returns AppSetting[key] if exists otherwise defaultValue, for non-string values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
public static T GetAppSetting<T>(string key, T defaultValue)
{
string val = ConfigurationManager.AppSettings[key];
if (val != null)
{
if (ConfigNullValue.EndsWith(val))
{
return default(T);
}
return ParseTextValue<T>(ConfigurationManager.AppSettings[key]);
}
return defaultValue;
}
/// <summary>
/// Gets the connection string setting.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static ConnectionStringSettings GetConnectionStringSetting(string key)
{
var value = ConfigurationManager.ConnectionStrings[key];
if (value == null)
{
throw new ConfigurationErrorsException(String.Format(ErrorConnectionStringNotFound, key));
}
return value;
}
/// <summary>
/// Gets the connection string.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static string GetConnectionString(string key)
{
return GetConnectionStringSetting(key).ToString();
}
/// <summary>
/// Gets the list from app setting.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static List<string> GetListFromAppSetting(string key)
{
return new List<string>(GetAppSetting(key).Split(ItemSeperator));
}
/// <summary>
/// Gets the dictionary from app setting.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static Dictionary<string, string> GetDictionaryFromAppSetting(string key)
{
var dictionary = new Dictionary<string, string>();
foreach (var item in GetAppSetting(key).Split(ItemSeperator))
{
var keyValuePair = item.Split(KeyValueSeperator);
dictionary.Add(keyValuePair[KeyIndex], keyValuePair[ValueIndex]);
}
return dictionary;
}
#region Private Methods
/// <summary>
/// Get the static Parse(string) method on the type supplied
/// </summary>
/// <param name="type"></param>
/// <returns>A delegate to the type's Parse(string) if it has one</returns>
private static MethodInfo GetParseMethod(Type type)
{
const string parseMethod = "Parse";
if (type == typeof(string))
{
return typeof(ConfigUtils).GetMethod(parseMethod, BindingFlags.Public | BindingFlags.Static);
}
var parseMethodInfo = type.GetMethod(parseMethod,
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(string) }, null);
return parseMethodInfo;
}
/// <summary>
/// Gets the constructor info for T(string) if exists.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
private static ConstructorInfo GetConstructorInfo(Type type)
{
foreach (ConstructorInfo ci in type.GetConstructors())
{
var ciTypes = ci.GetGenericArguments();
var matchFound = (ciTypes.Length == 1 && ciTypes[0] == typeof(string)); //e.g. T(string)
if (matchFound)
{
return ci;
}
}
return null;
}
/// <summary>
/// Returns the value returned by the 'T.Parse(string)' method if exists otherwise 'new T(string)'.
/// e.g. if T was a TimeSpan it will return TimeSpan.Parse(textValue).
/// If there is no Parse Method it will attempt to create a new instance of the destined type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="textValue">The default value.</param>
/// <returns>T.Parse(string) or new T(string) value</returns>
private static T ParseTextValue<T>(string textValue)
{
var parseMethod = GetParseMethod(typeof(T));
if (parseMethod == null)
{
var ci = GetConstructorInfo(typeof(T));
if (ci == null)
{
throw new TypeLoadException(string.Format(ErrorCreatingType, typeof(T).Name, textValue));
}
var newT = ci.Invoke(null, new object[] { textValue });
return (T)newT;
}
var value = parseMethod.Invoke(null, new object[] { textValue });
return (T)value;
}
#endregion
}
}