/
ConfigurationService.cs
417 lines (361 loc) · 15.6 KB
/
ConfigurationService.cs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigurationService.cs" company="Catel development team">
// Copyright (c) 2008 - 2015 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Catel.Configuration
{
using Runtime.Serialization;
using Services;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using Data;
using Catel.Logging;
using Runtime.Serialization.Xml;
#if PCL
// Not supported
#elif NETFX_CORE
using Windows.Storage;
#else
using System.Configuration;
using System.Linq;
using Path = IO.Path;
#endif
/// <summary>
/// Configuration service implementation that allows customization how configuration values
/// are being used inside an application.
/// <para />
/// This default implementation writes to the
/// </summary>
public partial class ConfigurationService : IConfigurationService
{
private static readonly string DefaultLocalConfigFilePath = Path.Combine(Path.GetApplicationDataDirectory(Catel.IO.ApplicationDataTarget.UserLocal), "configuration.xml");
private static readonly string DefaultRoamingConfigFilePath = Path.Combine(Path.GetApplicationDataDirectory(Catel.IO.ApplicationDataTarget.UserRoaming), "configuration.xml");
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
private readonly ISerializationManager _serializationManager;
private readonly IObjectConverterService _objectConverterService;
private readonly ISerializer _serializer;
#if NET || NETSTANDARD
private DynamicConfiguration _localConfiguration;
private DynamicConfiguration _roamingConfiguration;
private string _localConfigFilePath;
private string _roamingConfigFilePath;
#elif ANDROID
private readonly global::Android.Content.ISharedPreferences _preferences =
global::Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(global::Android.App.Application.Context);
#endif
private bool _suspendNotifications = false;
private bool _hasPendingNotifications = false;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationService" /> class.
/// </summary>
/// <param name="serializationManager">The serialization manager.</param>
/// <param name="objectConverterService">The object converter service.</param>
/// <param name="serializer">The serializer.</param>
public ConfigurationService(ISerializationManager serializationManager,
IObjectConverterService objectConverterService, IXmlSerializer serializer)
: this(serializationManager, objectConverterService, (ISerializer)serializer)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationService" /> class.
/// </summary>
/// <param name="serializationManager">The serialization manager.</param>
/// <param name="objectConverterService">The object converter service.</param>
/// <param name="serializer">The serializer.</param>
public ConfigurationService(ISerializationManager serializationManager,
IObjectConverterService objectConverterService, ISerializer serializer)
{
Argument.IsNotNull("serializationManager", serializationManager);
Argument.IsNotNull("objectConverterService", objectConverterService);
Argument.IsNotNull("serializer", serializer);
_serializationManager = serializationManager;
_objectConverterService = objectConverterService;
_serializer = serializer;
#if NET || NETSTANDARD
SetLocalConfigFilePath(DefaultLocalConfigFilePath);
SetRoamingConfigFilePath(DefaultRoamingConfigFilePath);
#endif
}
#region Events
/// <summary>
/// Occurs when the configuration has changed.
/// </summary>
public event EventHandler<ConfigurationChangedEventArgs> ConfigurationChanged;
#endregion
#region Methods
/// <summary>
/// Suspends the notifications of this service until the returned object is disposed.
/// </summary>
/// <returns>IDisposable.</returns>
public IDisposable SuspendNotifications()
{
return new DisposableToken<ConfigurationService>(this,
x =>
{
x.Instance._suspendNotifications = true;
},
x =>
{
x.Instance._suspendNotifications = false;
if (x.Instance._hasPendingNotifications)
{
x.Instance.RaiseConfigurationChanged(ConfigurationContainer.Roaming, string.Empty, string.Empty);
x.Instance._hasPendingNotifications = false;
}
});
}
/// <summary>
/// Gets the configuration value.
/// </summary>
/// <typeparam name="T">The type of the value to retrieve.</typeparam>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value. Will be returned if the value cannot be found.</param>
/// <returns>The configuration value.</returns>
/// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
public T GetValue<T>(ConfigurationContainer container, string key, T defaultValue = default(T))
{
Argument.IsNotNullOrWhitespace("key", key);
key = GetFinalKey(key);
try
{
if (!ValueExists(container, key))
{
return defaultValue;
}
var value = GetValueFromStore(container, key);
if (value == null)
{
return defaultValue;
}
// ObjectConverterService doesn't support object, but just return the value as is
if (typeof(T) == typeof(object))
{
return (T)(object)value;
}
return (T)_objectConverterService.ConvertFromStringToObject(value, typeof(T), CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
Log.Warning(ex, $"Failed to retrieve configuration value '{container}.{key}', returning default value");
return defaultValue;
}
}
/// <summary>
/// Sets the configuration value.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
public void SetValue(ConfigurationContainer container, string key, object value)
{
Argument.IsNotNullOrWhitespace("key", key);
var originalKey = key;
key = GetFinalKey(key);
var stringValue = _objectConverterService.ConvertFromObjectToString(value, CultureInfo.InvariantCulture);
var existingValue = GetValueFromStore(container, key);
SetValueToStore(container, key, stringValue);
if (!string.Equals(stringValue, existingValue))
{
RaiseConfigurationChanged(container, originalKey, value);
}
}
/// <summary>
/// Determines whether the specified value is available.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the specified value is available; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
public bool IsValueAvailable(ConfigurationContainer container, string key)
{
Argument.IsNotNullOrWhitespace("key", key);
key = GetFinalKey(key);
return ValueExists(container, key);
}
/// <summary>
/// Initializes the value by setting the value to the <paramref name="defaultValue" /> if the value does not yet exist.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
public void InitializeValue(ConfigurationContainer container, string key, object defaultValue)
{
Argument.IsNotNullOrWhitespace("key", key);
if (!IsValueAvailable(container, key))
{
SetValue(container, key, defaultValue);
}
}
#if NET || NETSTANDARD
/// <summary>
/// Sets the roaming config file path.
/// </summary>
/// <param name="filePath">The file path. </param>
public void SetRoamingConfigFilePath(string filePath)
{
Argument.IsNotNullOrEmpty(nameof(filePath), filePath);
_roamingConfigFilePath = filePath;
try
{
if (File.Exists(_roamingConfigFilePath))
{
using (var fileStream = new FileStream(_roamingConfigFilePath, FileMode.Open))
{
_roamingConfiguration = SavableModelBase<DynamicConfiguration>.Load(fileStream, _serializer);
}
}
else
{
_roamingConfiguration?.SaveAsXml(_roamingConfigFilePath);
}
}
catch (Exception ex)
{
Log.Error(ex, "Failed to load roaming configuration, using default settings");
_roamingConfigFilePath = DefaultRoamingConfigFilePath;
}
if (_roamingConfiguration == null)
{
_roamingConfiguration = new DynamicConfiguration();
}
}
/// <summary>
/// Sets the roaming config file path.
/// </summary>
/// <param name="filePath">The file path. </param>
public void SetLocalConfigFilePath(string filePath)
{
Argument.IsNotNullOrEmpty(nameof(filePath), filePath);
_localConfigFilePath = filePath;
try
{
if (File.Exists(_localConfigFilePath))
{
using (var fileStream = new FileStream(_localConfigFilePath, FileMode.Open))
{
_localConfiguration = SavableModelBase<DynamicConfiguration>.Load(fileStream, _serializer);
}
}
else
{
_localConfiguration?.SaveAsXml(_localConfigFilePath);
}
}
catch (Exception ex)
{
Log.Error(ex, "Failed to load local configuration, using default settings");
_localConfigFilePath = DefaultLocalConfigFilePath;
}
if (_localConfiguration == null)
{
_localConfiguration = new DynamicConfiguration();
}
}
#endif
/// <summary>
/// Determines whether the specified key value exists in the configuration.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the value exists, <c>false</c> otherwise.</returns>
protected virtual bool ValueExists(ConfigurationContainer container, string key)
{
#if PCL || (XAMARIN && !ANDROID)
throw Log.ErrorAndCreateException<NotSupportedInPlatformException>("No configuration objects available");
#elif ANDROID
return _preferences.Contains(key);
#elif NETFX_CORE
var settings = GetSettingsContainer(container);
return settings.Values.ContainsKey(key);
#else
var settings = GetSettingsContainer(container);
return settings.IsConfigurationValueSet(key);
#endif
}
/// <summary>
/// Gets the value from the store.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <returns>The value.</returns>
protected virtual string GetValueFromStore(ConfigurationContainer container, string key)
{
#if PCL || (XAMARIN && !ANDROID)
throw Log.ErrorAndCreateException<NotSupportedInPlatformException>("No configuration objects available");
#elif ANDROID
return _preferences.GetString(key, null);
#elif NETFX_CORE
var settings = GetSettingsContainer(container);
return (string)settings.Values[key];
#else
var settings = GetSettingsContainer(container);
return settings.GetConfigurationValue<string>(key, string.Empty);
#endif
}
/// <summary>
/// Sets the value to the store.
/// </summary>
/// <param name="container">The container.</param>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
protected virtual void SetValueToStore(ConfigurationContainer container, string key, string value)
{
#if PCL || (XAMARIN && !ANDROID)
throw Log.ErrorAndCreateException<NotSupportedInPlatformException>("No configuration objects available");
#elif ANDROID
_preferences.Edit()
.PutString(key, value)
.Apply();
#elif NETFX_CORE
var settings = GetSettingsContainer(container);
settings.Values[key] = value;
#else
var settings = GetSettingsContainer(container);
if (!settings.IsConfigurationValueSet(key))
{
settings.RegisterConfigurationKey(key);
}
settings.SetConfigurationValue(key, value);
string fileName = string.Empty;
switch (container)
{
case ConfigurationContainer.Local:
fileName = _localConfigFilePath;
break;
case ConfigurationContainer.Roaming:
fileName = _roamingConfigFilePath;
break;
default:
throw new ArgumentOutOfRangeException("container");
}
settings.SaveAsXml(fileName);
#endif
}
/// <summary>
/// Gets the final key. This method allows customization of the key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>System.String.</returns>
protected virtual string GetFinalKey(string key)
{
key = key.Replace(" ", "_");
return key;
}
private void RaiseConfigurationChanged(ConfigurationContainer container, string key, object value)
{
if (_suspendNotifications)
{
_hasPendingNotifications = true;
return;
}
ConfigurationChanged.SafeInvoke(this, () => new ConfigurationChangedEventArgs(container, key, value));
}
#endregion
}
}