All pages
Powered by GitBook
1 of 1

Loading...

Make settings required

Requires: abstractions module.

By default all settings are optional; that is, absence of relevant data in the source results in default values during binding and does not produce errors.

Some parts of configuration may be crucial to the application to the point that it's pointless to start without them being initialized. These fields and properties should be annotated with [Required] attribute:

provider.Get<MySettings>(); // will throw if DbConnectionString or Secrets are not initialized 

Changing defaults

Default behavior can be inverted in the scope of a type with [RequiredByDefault] attribute. Individual fields and properties can then be made optional with [Optional] attribute:

Related pages

class MySettings 
{
    [Required]
    public string DbConnectionString { get; }
    
    [Required]
    public MySecrets Secrets { get; }
}

class MySecrets 
{
    public string ApiKey { get; }
    public string Login { get; }
    public string Password { get; }
}
Obtain settings from provider
Observe settings via provider
Binding nodes to models
[RequiredByDefault]
class MySettings 
{
    [Optional]
    public TimeSpan Timeout { get; } = TimeSpan.FromSeconds(30);
}