Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SettingsService.cs
4
6{
10 [Singleton]
11 internal sealed class SettingsService : ISettingsService
12 {
16 public SettingsService()
17 {
18 }
19
20 private static void AssertNonEmptyPRefix(string KeyPrefix)
21 {
22 if (string.IsNullOrWhiteSpace(KeyPrefix))
23 throw new ArgumentException("Empty key prefix not permitted.", nameof(KeyPrefix));
24 }
25
31 public async Task SaveState(string Key, string State)
32 {
33 try
34 {
35 await RuntimeSettings.SetAsync(Key, State);
36 await Database.Provider.Flush();
37 }
38 catch (Exception ex)
39 {
40 ServiceRef.LogService.LogException(ex);
41 }
42 }
43
49 public async Task SaveState(string Key, long State)
50 {
51 try
52 {
53 await RuntimeSettings.SetAsync(Key, State);
54 await Database.Provider.Flush();
55 }
56 catch (Exception ex)
57 {
58 ServiceRef.LogService.LogException(ex);
59 }
60 }
61
67 public async Task SaveState(string Key, double State)
68 {
69 try
70 {
71 await RuntimeSettings.SetAsync(Key, State);
72 await Database.Provider.Flush();
73 }
74 catch (Exception ex)
75 {
76 ServiceRef.LogService.LogException(ex);
77 }
78 }
79
85 public async Task SaveState(string Key, bool State)
86 {
87 try
88 {
89 await RuntimeSettings.SetAsync(Key, State);
90 await Database.Provider.Flush();
91 }
92 catch (Exception ex)
93 {
94 ServiceRef.LogService.LogException(ex);
95 }
96 }
97
103 public async Task SaveState(string Key, DateTime State)
104 {
105 try
106 {
107 await RuntimeSettings.SetAsync(Key, State);
108 await Database.Provider.Flush();
109 }
110 catch (Exception ex)
111 {
112 ServiceRef.LogService.LogException(ex);
113 }
114 }
115
121 public async Task SaveState(string Key, TimeSpan State)
122 {
123 try
124 {
125 await RuntimeSettings.SetAsync(Key, State);
126 await Database.Provider.Flush();
127 }
128 catch (Exception ex)
129 {
130 ServiceRef.LogService.LogException(ex);
131 }
132 }
133
139 public async Task SaveState(string Key, Enum State)
140 {
141 try
142 {
143 await RuntimeSettings.SetAsync(Key, State);
144 await Database.Provider.Flush();
145 }
146 catch (Exception ex)
147 {
148 ServiceRef.LogService.LogException(ex);
149 }
150 }
151
157 public async Task SaveState(string Key, object State)
158 {
159 try
160 {
161 await RuntimeSettings.SetAsync(Key, State);
162 await Database.Provider.Flush();
163 }
164 catch (Exception ex)
165 {
166 ServiceRef.LogService.LogException(ex);
167 }
168 }
169
176 public async Task<string?> RestoreStringState(string Key, string? DefaultValueIfNotFound = default)
177 {
178 try
179 {
180 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
181 }
182 catch (Exception ex)
183 {
184 ServiceRef.LogService.LogException(ex);
185 }
186
187 return DefaultValueIfNotFound;
188 }
189
196 public async Task<long> RestoreLongState(string Key, long DefaultValueIfNotFound = default)
197 {
198 try
199 {
200 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
201 }
202 catch (Exception ex)
203 {
204 ServiceRef.LogService.LogException(ex);
205 }
206
207 return DefaultValueIfNotFound;
208 }
209
216 public async Task<double> RestoreDoubleState(string Key, double DefaultValueIfNotFound = default)
217 {
218 try
219 {
220 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
221 }
222 catch (Exception ex)
223 {
224 ServiceRef.LogService.LogException(ex);
225 }
226
227 return DefaultValueIfNotFound;
228 }
229
236 public async Task<bool> RestoreBoolState(string Key, bool DefaultValueIfNotFound = default)
237 {
238 try
239 {
240 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
241 }
242 catch (Exception ex)
243 {
244 ServiceRef.LogService.LogException(ex);
245 }
246
247 return DefaultValueIfNotFound;
248 }
249
256 public async Task<DateTime> RestoreDateTimeState(string Key, DateTime DefaultValueIfNotFound = default)
257 {
258 try
259 {
260 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
261 }
262 catch (Exception ex)
263 {
264 ServiceRef.LogService.LogException(ex);
265 }
266
267 return DefaultValueIfNotFound;
268 }
269
276 public async Task<TimeSpan> RestoreTimeSpanState(string Key, TimeSpan DefaultValueIfNotFound = default)
277 {
278 try
279 {
280 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
281 }
282 catch (Exception ex)
283 {
284 ServiceRef.LogService.LogException(ex);
285 }
286
287 return DefaultValueIfNotFound;
288 }
289
296 public async Task<Enum?> RestoreEnumState(string Key, Enum? DefaultValueIfNotFound = default)
297 {
298 try
299 {
300 return await RuntimeSettings.GetAsync(Key, DefaultValueIfNotFound);
301 }
302 catch (Exception ex)
303 {
304 ServiceRef.LogService.LogException(ex);
305 }
306
307 return DefaultValueIfNotFound;
308 }
309
317 public async Task<T?> RestoreState<T>(string Key, T? DefaultValueIfNotFound = default)
318 {
319 if (string.IsNullOrWhiteSpace(Key))
320 return DefaultValueIfNotFound;
321
322 try
323 {
324 object existingState = await RuntimeSettings.GetAsync(Key, (object?)null);
325
326 if (existingState is T TypedValue)
327 return TypedValue;
328
329 return DefaultValueIfNotFound;
330 }
331 catch (Exception ex)
332 {
333 ServiceRef.LogService.LogException(ex);
334 }
335
336 return DefaultValueIfNotFound;
337 }
338
343 public async Task RemoveState(string Key)
344 {
345 AssertNonEmptyPRefix(Key);
346
347 try
348 {
349 await RuntimeSettings.DeleteAsync(Key);
350 await Database.Provider.Flush();
351 }
352 catch (Exception ex)
353 {
354 ServiceRef.LogService.LogException(ex);
355 }
356 }
357
362 public Task<bool> WaitInitDone()
363 {
364 return ServiceRef.StorageService.WaitInitDone();
365 }
366 }
367}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static IDatabaseProvider Provider
Registered database provider.
Definition: Database.cs:57
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > DeleteAsync(string Key)
Deletes a runtime setting
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Task Flush()
Persists any pending changes.