Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RuntimeSettings.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
10
12{
16 public static class RuntimeSettings
17 {
18 #region String-valued settings
19
26 public static string Get(string Key, string DefaultValue)
27 {
28 return GetAsync(Key, DefaultValue).Result;
29 }
30
37 public static async Task<string> GetAsync(string Key, string DefaultValue)
38 {
39 StringSetting Setting = await GetAsync<StringSetting>(Key);
40 return Setting?.Value ?? DefaultValue;
41 }
42
43 private static async Task<T> GetAsync<T>(string Key)
44 where T : class
45 {
46 using (Semaphore Semaphore = await Semaphores.BeginRead("setting:" + Key))
47 {
48 T Setting = await Database.FindFirstDeleteRest<T>(new FilterFieldEqualTo("Key", Key));
49 return Setting;
50 }
51 }
52
59 public static bool Set(string Key, string Value)
60 {
61 return SetAsync(Key, Value).Result;
62 }
63
70 public static async Task<bool> SetAsync(string Key, string Value)
71 {
72 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
73 {
74 StringSetting Setting = await Database.FindFirstDeleteRest<StringSetting>(new FilterFieldEqualTo("Key", Key));
75 return await SetAsyncLocked(Key, Value, Setting);
76 }
77 }
78
79 private static async Task<bool> SetAsyncLocked(string Key, string Value, StringSetting Setting)
80 {
81 if (Setting is null)
82 {
83 Setting = new StringSetting(Key, Value);
84 await Database.Insert(Setting);
85
86 return true;
87 }
88 else
89 {
90 if (Setting.Value != Value)
91 {
92 Setting.Value = Value;
93 await Database.Update(Setting);
94
95 return true;
96 }
97 else
98 return false;
99 }
100 }
101
102 #endregion
103
104 #region Int64-valued settings
105
112 public static long Get(string Key, long DefaultValue)
113 {
114 return GetAsync(Key, DefaultValue).Result;
115 }
116
123 public static async Task<long> GetAsync(string Key, long DefaultValue)
124 {
125 Int64Setting Setting = await GetAsync<Int64Setting>(Key);
126 return Setting?.Value ?? DefaultValue;
127 }
128
135 public static bool Set(string Key, long Value)
136 {
137 return SetAsync(Key, Value).Result;
138 }
139
146 public static async Task<bool> SetAsync(string Key, long Value)
147 {
148 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
149 {
150 Int64Setting Setting = await Database.FindFirstDeleteRest<Int64Setting>(new FilterFieldEqualTo("Key", Key));
151 return await SetAsyncLocked(Key, Value, Setting);
152 }
153 }
154
155 private static async Task<bool> SetAsyncLocked(string Key, long Value, Int64Setting Setting)
156 {
157 if (Setting is null)
158 {
159 Setting = new Int64Setting(Key, Value);
160 await Database.Insert(Setting);
161
162 return true;
163 }
164 else
165 {
166 if (Setting.Value != Value)
167 {
168 Setting.Value = Value;
169 await Database.Update(Setting);
170
171 return true;
172 }
173 else
174 return false;
175 }
176 }
177
178 #endregion
179
180 #region Boolean-valued settings
181
188 public static bool Get(string Key, bool DefaultValue)
189 {
190 return GetAsync(Key, DefaultValue).Result;
191 }
192
199 public static async Task<bool> GetAsync(string Key, bool DefaultValue)
200 {
201 BooleanSetting Setting = await GetAsync<BooleanSetting>(Key);
202 return Setting?.Value ?? DefaultValue;
203 }
204
211 public static bool Set(string Key, bool Value)
212 {
213 return SetAsync(Key, Value).Result;
214 }
215
222 public static async Task<bool> SetAsync(string Key, bool Value)
223 {
224 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
225 {
226 BooleanSetting Setting = await Database.FindFirstDeleteRest<BooleanSetting>(new FilterFieldEqualTo("Key", Key));
227 return await SetAsyncLocked(Key, Value, Setting);
228 }
229 }
230
231 private static async Task<bool> SetAsyncLocked(string Key, bool Value, BooleanSetting Setting)
232 {
233 if (Setting is null)
234 {
235 Setting = new BooleanSetting(Key, Value);
236 await Database.Insert(Setting);
237
238 return true;
239 }
240 else
241 {
242 if (Setting.Value != Value)
243 {
244 Setting.Value = Value;
245 await Database.Update(Setting);
246
247 return true;
248 }
249 else
250 return false;
251 }
252 }
253
254 #endregion
255
256 #region DateTime-valued settings
257
264 public static DateTime Get(string Key, DateTime DefaultValue)
265 {
266 return GetAsync(Key, DefaultValue).Result;
267 }
268
275 public static async Task<DateTime> GetAsync(string Key, DateTime DefaultValue)
276 {
277 DateTimeSetting Setting = await GetAsync<DateTimeSetting>(Key);
278 return Setting?.Value ?? DefaultValue;
279 }
280
287 public static bool Set(string Key, DateTime Value)
288 {
289 return SetAsync(Key, Value).Result;
290 }
291
298 public static async Task<bool> SetAsync(string Key, DateTime Value)
299 {
300 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
301 {
302 DateTimeSetting Setting = await Database.FindFirstDeleteRest<DateTimeSetting>(new FilterFieldEqualTo("Key", Key));
303 return await SetAsyncLocked(Key, Value, Setting);
304 }
305 }
306
307 private static async Task<bool> SetAsyncLocked(string Key, DateTime Value, DateTimeSetting Setting)
308 {
309 if (Setting is null)
310 {
311 Setting = new DateTimeSetting(Key, Value);
312 await Database.Insert(Setting);
313
314 return true;
315 }
316 else
317 {
318 if (Setting.Value != Value)
319 {
320 Setting.Value = Value;
321 await Database.Update(Setting);
322
323 return true;
324 }
325 else
326 return false;
327 }
328 }
329
330 #endregion
331
332 #region TimeSpan-valued settings
333
340 public static TimeSpan Get(string Key, TimeSpan DefaultValue)
341 {
342 return GetAsync(Key, DefaultValue).Result;
343 }
344
351 public static async Task<TimeSpan> GetAsync(string Key, TimeSpan DefaultValue)
352 {
353 TimeSpanSetting Setting = await GetAsync<TimeSpanSetting>(Key);
354 return Setting?.Value ?? DefaultValue;
355 }
356
363 public static bool Set(string Key, TimeSpan Value)
364 {
365 return SetAsync(Key, Value).Result;
366 }
367
374 public static async Task<bool> SetAsync(string Key, TimeSpan Value)
375 {
376 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
377 {
378 TimeSpanSetting Setting = await Database.FindFirstDeleteRest<TimeSpanSetting>(new FilterFieldEqualTo("Key", Key));
379 return await SetAsyncLocked(Key, Value, Setting);
380 }
381 }
382
383 private static async Task<bool> SetAsyncLocked(string Key, TimeSpan Value, TimeSpanSetting Setting)
384 {
385 if (Setting is null)
386 {
387 Setting = new TimeSpanSetting(Key, Value);
388 await Database.Insert(Setting);
389
390 return true;
391 }
392 else
393 {
394 if (Setting.Value != Value)
395 {
396 Setting.Value = Value;
397 await Database.Update(Setting);
398
399 return true;
400 }
401 else
402 return false;
403 }
404 }
405
406 #endregion
407
408 #region Double-valued settings
409
416 public static double Get(string Key, double DefaultValue)
417 {
418 return GetAsync(Key, DefaultValue).Result;
419 }
420
427 public static async Task<double> GetAsync(string Key, double DefaultValue)
428 {
429 DoubleSetting Setting = await GetAsync<DoubleSetting>(Key);
430 return Setting?.Value ?? DefaultValue;
431 }
432
439 public static bool Set(string Key, double Value)
440 {
441 return SetAsync(Key, Value).Result;
442 }
443
450 public static async Task<bool> SetAsync(string Key, double Value)
451 {
452 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
453 {
454 DoubleSetting Setting = await Database.FindFirstDeleteRest<DoubleSetting>(new FilterFieldEqualTo("Key", Key));
455 return await SetAsyncLocked(Key, Value, Setting);
456 }
457 }
458
459 private static async Task<bool> SetAsyncLocked(string Key, double Value, DoubleSetting Setting)
460 {
461 if (Setting is null)
462 {
463 Setting = new DoubleSetting(Key, Value);
464 await Database.Insert(Setting);
465
466 return true;
467 }
468 else
469 {
470 if (Setting.Value != Value)
471 {
472 Setting.Value = Value;
473 await Database.Update(Setting);
474
475 return true;
476 }
477 else
478 return false;
479 }
480 }
481
482 #endregion
483
484 #region Enum-valued settings
485
492 public static Enum Get(string Key, Enum DefaultValue)
493 {
494 return GetAsync(Key, DefaultValue).Result;
495 }
496
503 public static async Task<Enum> GetAsync(string Key, Enum DefaultValue)
504 {
505 EnumSetting Setting = await GetAsync<EnumSetting>(Key);
506 return Setting?.Value ?? DefaultValue;
507 }
508
515 public static bool Set(string Key, Enum Value)
516 {
517 return SetAsync(Key, Value).Result;
518 }
519
526 public static async Task<bool> SetAsync(string Key, Enum Value)
527 {
528 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
529 {
530 EnumSetting Setting = await Database.FindFirstDeleteRest<EnumSetting>(new FilterFieldEqualTo("Key", Key));
531 return await SetAsyncLocked(Key, Value, Setting);
532 }
533 }
534
535 private static async Task<bool> SetAsyncLocked(string Key, Enum Value, EnumSetting Setting)
536 {
537 if (Setting is null)
538 {
539 Setting = new EnumSetting(Key, Value);
540 await Database.Insert(Setting);
541
542 return true;
543 }
544 else
545 {
546 if (Setting.Value != Value)
547 {
548 Setting.Value = Value;
549 await Database.Update(Setting);
550
551 return true;
552 }
553 else
554 return false;
555 }
556 }
557
558 #endregion
559
560 #region Object-valued settings
561
568 public static object Get(string Key, object DefaultValue)
569 {
570 return GetAsync(Key, DefaultValue).Result;
571 }
572
579 public static async Task<object> GetAsync(string Key, object DefaultValue)
580 {
581 Setting Setting = await GetAsync<Setting>(Key);
582 return Setting?.GetValueObject() ?? DefaultValue;
583 }
584
591 public static bool Set(string Key, object Value)
592 {
593 return SetAsync(Key, Value).Result;
594 }
595
602 public static async Task<bool> SetAsync(string Key, object Value)
603 {
604 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
605 {
606 Setting Setting = await Database.FindFirstDeleteRest<Setting>(new FilterFieldEqualTo("Key", Key));
607
608 if (Value is null)
609 {
610 if (!(Setting is ObjectSetting))
611 {
612 if (!(Setting is null))
613 await Database.Delete(Setting);
614
615 Setting = null;
616 }
617 }
618 else
619 {
620 if (Value is string s)
621 {
623 {
624 if (!(Setting is null))
625 await Database.Delete(Setting);
626
627 StringSetting = null;
628 }
629
630 return await SetAsyncLocked(Key, s, StringSetting);
631 }
632 else if (Value is long l)
633 {
635 {
636 if (!(Setting is null))
637 await Database.Delete(Setting);
638
639 Int64Setting = null;
640 }
641
642 return await SetAsyncLocked(Key, l, Int64Setting);
643 }
644 else if (Value is double d)
645 {
647 {
648 if (!(Setting is null))
649 await Database.Delete(Setting);
650
651 DoubleSetting = null;
652 }
653
654 return await SetAsyncLocked(Key, d, DoubleSetting);
655 }
656 else if (Value is bool b)
657 {
659 {
660 if (!(Setting is null))
661 await Database.Delete(Setting);
662
663 BooleanSetting = null;
664 }
665
666 return await SetAsyncLocked(Key, b, BooleanSetting);
667 }
668 else if (Value is DateTime TP)
669 {
671 {
672 if (!(Setting is null))
673 await Database.Delete(Setting);
674
675 DateTimeSetting = null;
676 }
677
678 return await SetAsyncLocked(Key, TP, DateTimeSetting);
679 }
680 else if (Value is TimeSpan TS)
681 {
683 {
684 if (!(Setting is null))
685 await Database.Delete(Setting);
686
687 TimeSpanSetting = null;
688 }
689
690 return await SetAsyncLocked(Key, TS, TimeSpanSetting);
691 }
692 else if (Value is Enum E)
693 {
695 {
696 if (!(Setting is null))
697 await Database.Delete(Setting);
698
699 EnumSetting = null;
700 }
701
702 return await SetAsyncLocked(Key, E, EnumSetting);
703 }
704 else
705 {
706 Type T = Value.GetType();
707 TypeInfo TI = T.GetTypeInfo();
708
709 if (!(TI.GetCustomAttribute(typeof(CollectionNameAttribute)) is null))
710 throw new InvalidOperationException("Object setting values cannot be stored separate collections. (CollectionName attribute found.)");
711
712 TypeNameAttribute TypeNameAttribute = TI.GetCustomAttribute<TypeNameAttribute>();
714 throw new InvalidOperationException("Full Type names must be serialized when persisting object setting values. (TypeName attribute.). Exceptions for the types: Boolean, Int64, String, DateTime, TimeSpan, Double.");
715 }
716 }
717
719 {
720 if (((ObjectSetting.Value is null) ^ (Value is null)) || !ObjectSetting.Value.Equals(Value))
721 {
722 ObjectSetting.Value = Value;
724
725 return true;
726 }
727 else
728 return false;
729 }
730 else
731 {
732 Setting = new ObjectSetting(Key, Value);
733 await Database.Insert(Setting);
734
735 return true;
736 }
737 }
738 }
739
740 #endregion
741
742 #region Delete Setting
743
749 public static async Task<bool> DeleteAsync(string Key)
750 {
751 using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
752 {
753 foreach (Setting Setting in await Database.FindDelete<Setting>(new FilterFieldEqualTo("Key", Key)))
754 return true;
755
756 return false;
757 }
758 }
759
760 #endregion
761
762 #region Batch Get
763
769 public static Dictionary<string, object> GetWhere(Filter Filter)
770 {
771 return GetWhereAsync(Filter).Result;
772 }
773
779 public static async Task<Dictionary<string, object>> GetWhereAsync(Filter Filter)
780 {
781 Dictionary<string, object> Result = new Dictionary<string, object>();
782
783 foreach (Setting Setting in await Database.Find<Setting>(Filter))
784 Result[Setting.Key] = Setting.GetValueObject();
785
786 return Result;
787 }
788
794 public static Dictionary<string, object> GetWhereKeyLikeRegEx(string KeyPattern)
795 {
796 return GetWhere(new FilterFieldLikeRegEx("Key", KeyPattern));
797 }
798
804 public static Task<Dictionary<string, object>> GetWhereKeyLikeRegExAsync(string KeyPattern)
805 {
806 return GetWhereAsync(new FilterFieldLikeRegEx("Key", KeyPattern));
807 }
808
815 public static Dictionary<string, object> GetWhereKeyLike(string Key, string Wildcard)
816 {
817 return GetWhere(new FilterFieldLikeRegEx("Key", Database.WildcardToRegex(Key, Wildcard)));
818 }
819
826 public static Task<Dictionary<string, object>> GetWhereKeyLikeAsync(string Key, string Wildcard)
827 {
828 return GetWhereAsync(new FilterFieldLikeRegEx("Key", Database.WildcardToRegex(Key, Wildcard)));
829 }
830
831 #endregion
832
833 #region Batch Delete
834
840 public static int DeleteWhere(Filter Filter)
841 {
842 return DeleteWhereAsync(Filter).Result;
843 }
844
850 public static async Task<int> DeleteWhereAsync(Filter Filter)
851 {
852 int Result = 0;
853
854 foreach (Setting Setting in await Database.FindDelete<Setting>(Filter))
855 Result++;
856
857 return Result;
858 }
859
865 public static int DeleteWhereKeyLikeRegEx(string KeyPattern)
866 {
867 return DeleteWhere(new FilterFieldLikeRegEx("Key", KeyPattern));
868 }
869
875 public static Task<int> DeleteWhereKeyLikeRegExAsync(string KeyPattern)
876 {
877 return DeleteWhereAsync(new FilterFieldLikeRegEx("Key", KeyPattern));
878 }
879
886 public static int DeleteWhereKeyLike(string Key, string Wildcard)
887 {
888 return DeleteWhere(new FilterFieldLikeRegEx("Key", Database.WildcardToRegex(Key, Wildcard)));
889 }
890
897 public static Task<int> DeleteWhereKeyLikeAsync(string Key, string Wildcard)
898 {
899 return DeleteWhereAsync(new FilterFieldLikeRegEx("Key", Database.WildcardToRegex(Key, Wildcard)));
900 }
901
902 #endregion
903 }
904}
This attribute defines the name of the collection that will house objects of this type.
This attribute defines the name of the collection that will house objects of this type.
TypeNameSerialization TypeNameSerialization
How the type name should be serialized.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
Definition: Database.cs:879
static string WildcardToRegex(string s, string Wildcard)
Converts a wildcard string to a regular expression string.
Definition: Database.cs:1631
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:717
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field matching a given regular expression.
Base class for all filter classes.
Definition: Filter.cs:15
Static class managing persistent settings.
static double Get(string Key, double DefaultValue)
Gets a double-valued setting.
static async Task< bool > SetAsync(string Key, double Value)
Sets a double-valued setting.
static object Get(string Key, object DefaultValue)
Gets a object-valued setting.
static bool Set(string Key, object Value)
Sets a object-valued setting.
static Task< Dictionary< string, object > > GetWhereKeyLikeRegExAsync(string KeyPattern)
Gets available settings, matching a search filter.
static int DeleteWhereKeyLikeRegEx(string KeyPattern)
Deletes available settings, matching a search filter.
static int DeleteWhere(Filter Filter)
Deletes available settings, matching a search filter.
static TimeSpan Get(string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static bool Set(string Key, DateTime Value)
Sets a DateTime-valued setting.
static Task< int > DeleteWhereKeyLikeAsync(string Key, string Wildcard)
Deletes available settings, matching a search filter.
static Task< int > DeleteWhereKeyLikeRegExAsync(string KeyPattern)
Deletes available settings, matching a search filter.
static bool Set(string Key, string Value)
Sets a string-valued setting.
static bool Set(string Key, long Value)
Sets a long-valued setting.
static Enum Get(string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static async Task< Dictionary< string, object > > GetWhereAsync(Filter Filter)
Gets available settings, matching a search filter.
static async Task< int > DeleteWhereAsync(Filter Filter)
Deletes available settings, matching a search filter.
static async Task< long > GetAsync(string Key, long DefaultValue)
Gets a long-valued setting.
static bool Set(string Key, Enum Value)
Sets a Enum-valued setting.
static async Task< Enum > GetAsync(string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static bool Set(string Key, TimeSpan Value)
Sets a TimeSpan-valued setting.
static async Task< DateTime > GetAsync(string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static Task< Dictionary< string, object > > GetWhereKeyLikeAsync(string Key, string Wildcard)
Gets available settings, matching a search filter.
static async Task< bool > SetAsync(string Key, long Value)
Sets a long-valued setting.
static async Task< bool > SetAsync(string Key, TimeSpan Value)
Sets a TimeSpan-valued setting.
static string Get(string Key, string DefaultValue)
Gets a string-valued setting.
static bool Set(string Key, bool Value)
Sets a bool-valued setting.
static async Task< bool > SetAsync(string Key, Enum Value)
Sets a Enum-valued setting.
static async Task< bool > SetAsync(string Key, object Value)
Sets a object-valued setting.
static DateTime Get(string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static Dictionary< string, object > GetWhereKeyLike(string Key, string Wildcard)
Gets available settings, matching a search filter.
static async Task< object > GetAsync(string Key, object DefaultValue)
Gets a object-valued setting.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static Dictionary< string, object > GetWhere(Filter Filter)
Gets available settings, matching a search filter.
static async Task< bool > DeleteAsync(string Key)
Deletes a runtime setting
static async Task< double > GetAsync(string Key, double DefaultValue)
Gets a double-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
static int DeleteWhereKeyLike(string Key, string Wildcard)
Deletes available settings, matching a search filter.
static async Task< bool > SetAsync(string Key, bool Value)
Sets a bool-valued setting.
static bool Get(string Key, bool DefaultValue)
Gets a bool-valued setting.
static long Get(string Key, long DefaultValue)
Gets a long-valued setting.
static async Task< TimeSpan > GetAsync(string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static async Task< bool > GetAsync(string Key, bool DefaultValue)
Gets a bool-valued setting.
static bool Set(string Key, double Value)
Sets a double-valued setting.
static Dictionary< string, object > GetWhereKeyLikeRegEx(string KeyPattern)
Gets available settings, matching a search filter.
static async Task< bool > SetAsync(string Key, DateTime Value)
Sets a DateTime-valued setting.
Base abstract class for settings.
Definition: Setting.cs:14
abstract object GetValueObject()
Gets the value of the setting, as an object.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:16
static async Task< Semaphore > BeginRead(string Key)
Waits until the semaphore identified by Key is ready for reading. Each call to BeginRead must be fol...
Definition: Semaphores.cs:53
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:90
TypeNameSerialization
How the type name should be serialized.