-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started
Package Manager Console:
Install-Package DotNetNinja.AutoBoundConfiguration
.NET CLI
dotnet add package DotNetNinja.AutoBoundConfiguration
To get the library plumbed into your application you'll need to add a line into your Starup.cs file's ConfigureServices(...) method. You'll need to use the AddAutoBoundConfigurations(...) extension method of the services instance to do this. ex.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoBoundConfigurations(Configuration).FromAssembly(typeof(Program).Assembly);
services.AddControllersWithViews();
}- AddAutoBoundConfigurations(Configuration) => Creates an builder object from your configuration source.
- FromAssembly([Assembly]) => Scans the given assembly for classes marked with the [AutoBind("SECTIONNAME")] attribute, creates an instance of each of them, binds each of them to the configuration section identified by SECTIONNAME, and registers that instance in a singleton lifetime scope with the dependency injection system.
- If you need to bind classes from other assemblies you can:
- Chain FromAssembly(...) calls, one for each assembly OR
- Create an IEnumerable containing all of the assemblies and use the FromAssemblies(...) method instead (example below)
- If you need to bind classes from other assemblies you can:
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoBoundConfigurations(Configuration)
.FromAssembly(typeof(Program).Assembly)
.FromAssembly(typeof(TypeFromAssembly2).Assembly);
services.AddControllersWithViews();
}OR
public void ConfigureServices(IServiceCollection services)
{
var assemblies = new []
{
typeof(Program).Assembly,
typeof(TypeFromAssembly2).Assembly
};
services.AddAutoBoundConfigurations(Configuration).FromAssemblies(assemblies);
services.AddControllersWithViews();
}If you only need to add one type, you can use the For() method instead of the FromAssembly(...) method. This will be somewhat faster, but this code only is called once during application start up, and it defeats one of the primary purposes of this library which is to avoid having to come back to the Startup.cs to set up each type of configuration class.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoBoundConfigurations(Configuration)
.FromAssembly(typeof(Program).Assembly)
.For<TypeFromAssembly2>();
services.AddControllersWithViews();
}Unless you have a huge assembly to load from and/or you really need to keep start up time as absolutely low as possible, I would advise against using the For method.
You can see an example of this in the Sample Application's Startup.cs file.
Now that the plumbing code is done you can start creating classes and binding them to your configuration. Here is an example class:
[AutoBind("Features")]
public class Features
{
public bool IsCoolFeatureEnabled { get; set; }
public int CoolFeatureCount { get; set; }
public bool IsUberFeatureEnabled { get; set; }
public bool IsSecretFeatureEnabled { get; set; }
}The AutoBind attribute instructs the system to bind this class to a section named "Features" in your configuration. Take the following configuration JSON as an example:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Features": {
"IsCoolFeatureEnabled": true,
"CoolFeatureCount": 4,
"IsUberFeatureEnabled": true,
"IsSecretFeatureEnabled": false
},
"AllowedHosts": "*"
}In this case the Features section would be bound to the class and the values for each of the properties would be set to the values with a matching name in your configuration. Additionally, this class (specifically an instance of this class) will be configured in the dependency injection system as a singleton. All you need to do now to read/use the configuration values in your code is inject "Features" into an instance of a class that need to use them. ex:
public class HomeController : Controller
{
private readonly Features _features;
public HomeController(Features features)
{
_features = features ?? throw new ArgumentNullException(nameof(features));
}
...
}Now you have a reference to your settings and can use it anywhere in this controller (or what-ever class you have injected it into).
If you do not specify a configuration section by name in the AutoBind attribute on your class, the system will attempt to bind it to a section with the same name as your class. ex:
[AutoBind]
public class Features
{
public bool IsCoolFeatureEnabled { get; set; }
public int CoolFeatureCount { get; set; }
public bool IsUberFeatureEnabled { get; set; }
public bool IsSecretFeatureEnabled { get; set; }
}This will still bind to the same "Features" section of your configuration as before because the class is named Features.