2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
19 #region String-valued settings
28 public static string Get(
string Host,
string Key,
string DefaultValue)
30 return GetAsync(Host, Key, DefaultValue).Result;
40 public static async Task<string>
GetAsync(
string Host,
string Key,
string DefaultValue)
46 private static async Task<T> GetAsync<T>(
string Host,
string Key)
65 public static bool Set(
string Host,
string Key,
string Value)
67 return SetAsync(Host, Key, Value).Result;
77 public static async Task<bool>
SetAsync(
string Host,
string Key,
string Value)
83 return await SetAsyncLocked(Host, Key, Value, Setting);
87 private static async Task<bool> SetAsyncLocked(
string Host,
string Key,
string Value,
StringHostSetting Setting)
98 if (Setting.
Value != Value)
100 Setting.Value = Value;
112 #region Int64-valued settings
121 public static long Get(
string Host,
string Key,
long DefaultValue)
123 return GetAsync(Host, Key, DefaultValue).Result;
133 public static async Task<long>
GetAsync(
string Host,
string Key,
long DefaultValue)
146 public static bool Set(
string Host,
string Key,
long Value)
148 return SetAsync(Host, Key, Value).Result;
158 public static async Task<bool>
SetAsync(
string Host,
string Key,
long Value)
164 return await SetAsyncLocked(Host, Key, Value, Setting);
168 private static async Task<bool> SetAsyncLocked(
string Host,
string Key,
long Value,
Int64HostSetting Setting)
179 if (Setting.
Value != Value)
181 Setting.Value = Value;
193 #region Boolean-valued settings
202 public static bool Get(
string Host,
string Key,
bool DefaultValue)
204 return GetAsync(Host, Key, DefaultValue).Result;
214 public static async Task<bool>
GetAsync(
string Host,
string Key,
bool DefaultValue)
227 public static bool Set(
string Host,
string Key,
bool Value)
229 return SetAsync(Host, Key, Value).Result;
239 public static async Task<bool>
SetAsync(
string Host,
string Key,
bool Value)
245 return await SetAsyncLocked(Host, Key, Value, Setting);
249 private static async Task<bool> SetAsyncLocked(
string Host,
string Key,
bool Value,
BooleanHostSetting Setting)
260 if (Setting.
Value != Value)
262 Setting.Value = Value;
274 #region DateTime-valued settings
283 public static DateTime
Get(
string Host,
string Key, DateTime DefaultValue)
285 return GetAsync(Host, Key, DefaultValue).Result;
295 public static async Task<DateTime>
GetAsync(
string Host,
string Key, DateTime DefaultValue)
308 public static bool Set(
string Host,
string Key, DateTime Value)
310 return SetAsync(Host, Key, Value).Result;
320 public static async Task<bool>
SetAsync(
string Host,
string Key, DateTime Value)
326 return await SetAsyncLocked(Host, Key, Value, Setting);
330 private static async Task<bool> SetAsyncLocked(
string Host,
string Key, DateTime Value,
DateTimeHostSetting Setting)
341 if (Setting.
Value != Value)
343 Setting.Value = Value;
355 #region TimeSpan-valued settings
364 public static TimeSpan
Get(
string Host,
string Key, TimeSpan DefaultValue)
366 return GetAsync(Host, Key, DefaultValue).Result;
376 public static async Task<TimeSpan>
GetAsync(
string Host,
string Key, TimeSpan DefaultValue)
389 public static bool Set(
string Host,
string Key, TimeSpan Value)
391 return SetAsync(Host, Key, Value).Result;
401 public static async Task<bool>
SetAsync(
string Host,
string Key, TimeSpan Value)
407 return await SetAsyncLocked(Host, Key, Value, Setting);
411 private static async Task<bool> SetAsyncLocked(
string Host,
string Key, TimeSpan Value,
TimeSpanHostSetting Setting)
422 if (Setting.
Value != Value)
424 Setting.Value = Value;
436 #region Double-valued settings
445 public static double Get(
string Host,
string Key,
double DefaultValue)
447 return GetAsync(Host, Key, DefaultValue).Result;
457 public static async Task<double>
GetAsync(
string Host,
string Key,
double DefaultValue)
470 public static bool Set(
string Host,
string Key,
double Value)
472 return SetAsync(Host, Key, Value).Result;
482 public static async Task<bool>
SetAsync(
string Host,
string Key,
double Value)
488 return await SetAsyncLocked(Host, Key, Value, Setting);
492 private static async Task<bool> SetAsyncLocked(
string Host,
string Key,
double Value,
DoubleHostSetting Setting)
503 if (Setting.
Value != Value)
505 Setting.Value = Value;
517 #region Enum-valued settings
526 public static Enum
Get(
string Host,
string Key, Enum DefaultValue)
528 return GetAsync(Host, Key, DefaultValue).Result;
538 public static async Task<Enum>
GetAsync(
string Host,
string Key, Enum DefaultValue)
551 public static bool Set(
string Host,
string Key, Enum Value)
553 return SetAsync(Host, Key, Value).Result;
563 public static async Task<bool>
SetAsync(
string Host,
string Key, Enum Value)
569 return await SetAsyncLocked(Host, Key, Value, Setting);
573 private static async Task<bool> SetAsyncLocked(
string Host,
string Key, Enum Value,
EnumHostSetting Setting)
584 if (Setting.
Value != Value)
586 Setting.Value = Value;
598 #region Object-valued settings
607 public static object Get(
string Host,
string Key,
object DefaultValue)
609 return GetAsync(Host, Key, DefaultValue).Result;
619 public static async Task<object>
GetAsync(
string Host,
string Key,
object DefaultValue)
621 HostSetting Setting = await GetAsync<HostSetting>(Host, Key);
632 public static bool Set(
string Host,
string Key,
object Value)
634 return SetAsync(Host, Key, Value).Result;
644 public static async Task<bool>
SetAsync(
string Host,
string Key,
object Value)
655 if (!(Setting is
null))
663 if (Value is
string s)
667 if (!(Setting is
null))
670 StringSetting =
null;
673 return await SetAsyncLocked(Host, Key, s, StringSetting);
675 else if (Value is
long l)
679 if (!(Setting is
null))
685 return await SetAsyncLocked(Host, Key, l, Int64Setting);
687 else if (Value is
double d)
691 if (!(Setting is
null))
694 DoubleSetting =
null;
697 return await SetAsyncLocked(Host, Key, d, DoubleSetting);
699 else if (Value is
bool b)
703 if (!(Setting is
null))
706 BooleanSetting =
null;
709 return await SetAsyncLocked(Host, Key, b, BooleanSetting);
711 else if (Value is DateTime TP)
715 if (!(Setting is
null))
718 DateTimeSetting =
null;
721 return await SetAsyncLocked(Host, Key, TP, DateTimeSetting);
723 else if (Value is TimeSpan TS)
727 if (!(Setting is
null))
730 TimeSpanSetting =
null;
733 return await SetAsyncLocked(Host, Key, TS, TimeSpanSetting);
735 else if (Value is Enum E)
739 if (!(Setting is
null))
745 return await SetAsyncLocked(Host, Key, E, EnumSetting);
749 Type T = Value.GetType();
750 TypeInfo TI = T.GetTypeInfo();
753 throw new InvalidOperationException(
"Object setting values cannot be stored separate collections. (CollectionName attribute found.)");
757 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.");
763 if (((ObjectSetting.Value is
null) ^ (Value is
null)) || !ObjectSetting.Value.Equals(Value))
765 ObjectSetting.Value = Value;
785 #region Delete Setting
793 public static async Task<bool>
DeleteAsync(
string Host,
string Key)
832 Dictionary<string, object> Result =
new Dictionary<string, object>();
873 public static Dictionary<string, object>
GetWhereKeyLike(
string Host,
string Key,
string Wildcard)
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...
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
static string WildcardToRegex(string s, string Wildcard)
Converts a wildcard string to a regular expression string.
static async Task Update(object Object)
Updates an object in the database.
static async Task Delete(object Object)
Deletes an object in the database.
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that conform to all child-filters provided.
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.
Enumeration setting object.
Base abstract class for host settings.
abstract object GetValueObject()
Gets the value of the setting, as an object.
Static class managing persistent host settings. Host settings default to runtime settings if host-spe...
static Dictionary< string, object > GetWhereKeyLikeRegEx(string Host, string KeyPattern)
Gets available settings, matching a search filter, indexed by key.
static async Task< bool > SetAsync(string Host, string Key, Enum Value)
Sets a Enum-valued setting.
static Dictionary< string, object > GetHostValues(string Key)
Gets available settings for a given key, indexed by host.
static Task< Dictionary< string, object > > GetWhereKeyLikeAsync(string Host, string Key, string Wildcard)
Gets available settings, matching a search filter, indexed by key.
static Task< Dictionary< string, object > > GetWhereKeyLikeRegExAsync(string Host, string KeyPattern)
Gets available settings, matching a search filter, indexed by key.
static double Get(string Host, string Key, double DefaultValue)
Gets a double-valued setting.
static async Task< double > GetAsync(string Host, string Key, double DefaultValue)
Gets a double-valued setting.
static bool Set(string Host, string Key, string Value)
Sets a string-valued setting.
static async Task< bool > SetAsync(string Host, string Key, long Value)
Sets a long-valued setting.
static int DeleteWhereKeyLikeRegEx(string Host, string KeyPattern)
Deletes available settings, matching a search filter.
static TimeSpan Get(string Host, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static async Task< Enum > GetAsync(string Host, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static async Task< bool > GetAsync(string Host, string Key, bool DefaultValue)
Gets a bool-valued setting.
static async Task< bool > SetAsync(string Host, string Key, double Value)
Sets a double-valued setting.
static Task< Dictionary< string, object > > GetHostValuesAsync(string Key)
Gets available settings for a given key, indexed by host.
static bool Set(string Host, string Key, long Value)
Sets a long-valued setting.
static Dictionary< string, object > GetWhereKeyLike(string Host, string Key, string Wildcard)
Gets available settings, matching a search filter, indexed by key.
static bool Set(string Host, string Key, object Value)
Sets a object-valued setting.
static bool Set(string Host, string Key, bool Value)
Sets a bool-valued setting.
static async Task< DateTime > GetAsync(string Host, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static async Task< string > GetAsync(string Host, string Key, string DefaultValue)
Gets a string-valued setting.
static Enum Get(string Host, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static async Task< long > GetAsync(string Host, string Key, long DefaultValue)
Gets a long-valued setting.
static async Task< bool > SetAsync(string Host, string Key, object Value)
Sets a object-valued setting.
static async Task< TimeSpan > GetAsync(string Host, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static Task< int > DeleteWhereKeyLikeRegExAsync(string Host, string KeyPattern)
Deletes available settings, matching a search filter.
static Dictionary< string, object > GetWhere(Filter Filter, bool HostAsKey)
Gets available settings, matching a search filter.
static bool Set(string Host, string Key, TimeSpan Value)
Sets a TimeSpan-valued setting.
static Task< int > DeleteWhereKeyLikeAsync(string Host, string Key, string Wildcard)
Deletes available settings, matching a search filter.
static string Get(string Host, string Key, string DefaultValue)
Gets a string-valued setting.
static long Get(string Host, string Key, long DefaultValue)
Gets a long-valued setting.
static DateTime Get(string Host, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static async Task< int > DeleteWhereAsync(Filter Filter)
Deletes available settings, matching a search filter.
static async Task< bool > DeleteAsync(string Host, string Key)
Deletes a runtime setting
static bool Set(string Host, string Key, double Value)
Sets a double-valued setting.
static async Task< Dictionary< string, object > > GetWhereAsync(Filter Filter, bool HostAsKey)
Gets available settings, matching a search filter.
static async Task< object > GetAsync(string Host, string Key, object DefaultValue)
Gets a object-valued setting.
static async Task< bool > SetAsync(string Host, string Key, TimeSpan Value)
Sets a TimeSpan-valued setting.
static int DeleteWhereKeyLike(string Host, string Key, string Wildcard)
Deletes available settings, matching a search filter.
static async Task< bool > SetAsync(string Host, string Key, DateTime Value)
Sets a DateTime-valued setting.
static bool Get(string Host, string Key, bool DefaultValue)
Gets a bool-valued setting.
static async Task< bool > SetAsync(string Host, string Key, string Value)
Sets a string-valued setting.
static async Task< bool > SetAsync(string Host, string Key, bool Value)
Sets a bool-valued setting.
static bool Set(string Host, string Key, DateTime Value)
Sets a DateTime-valued setting.
static bool Set(string Host, string Key, Enum Value)
Sets a Enum-valued setting.
static int DeleteWhere(Filter Filter)
Deletes available settings, matching a search filter.
static object Get(string Host, string Key, object DefaultValue)
Gets a object-valued setting.
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Static class of application-wide semaphores that can be used to order access to editable objects.
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...
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...
TypeNameSerialization
How the type name should be serialized.