-
Notifications
You must be signed in to change notification settings - Fork 2
core.settings.overview
NuGet Package | NetFusion.Settings |
---|---|
Registration | services.CompositeAppBuilder().AddSettings().Compose(); |
Types | IAppSettings, [ConfigurationSection] |
Sample Code | NetFusion-Examples/Source/Settings |
dotnet add package NetFusion.Settings
This plug-in extends Microsoft's Configuration Extensions and allows for validation and direct injection of settings in dependent components.
Settings are classes containing properties, loaded at runtime, that can be injected into components. NetFusion is based on Microsoft's Configuration Extensions and provides the following:
- The NetFusion.Settings plug-in discovers all IAppSettings class definitions, representing application settings, from all registered plug-ins. The settings plug-in automatically registers the setting classes in IServiceCollection. When injected into dependent components, the settings are loaded by delegating to Microsoft's Configuration Extensions. Components can inject the application setting classes directly into their constructors.
- Instead of specifying the settings section path within the Start class as is often shown in examples, the path is specified directly on the settings type using the ConfigurationSection attribute.
- After settings are loaded, the state of the object is validated.
The use of settings will be illustrated by providing code examples. The code within this section assumes the solution within NetFusion-Examples/Examples/Source/Demo as the starting point.
Configuring the IConfigurationBuilder is the same as if NetFusion.Settings was not being used. Begin by adding the following NetFusion.Settings NuGet package to the Demo.App project.
dotnet add ./src/Components/Demo.App/Demo.App.csproj package NetFusion.Settings
Specify within the ConfigureServices method of the web application's Start class that the NetFusion.Settings plug-in should be added to the composite-container by calling the AddSettings method as follows:
nano ./src/Demo.WebApi/Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.CompositeContainer(_configuration, new NullExtendedLogger())
// Register Additional Plugins Here:
.AddSettings()
.AddPlugin<InfraPlugin>()
.AddPlugin<AppPlugin>()
.AddPlugin<DomainPlugin>()
.AddPlugin<WebApiPlugin>()
.Compose();
services.AddControllers();
}
Within the Web application's Program class, add configuration sources as normal when building the host. In the below example, the following line of code as added:
builder.AddAppSettings(context.HostingEnvironment);
This line of code calls an extension method provided by NetFusion that adds the common configuration sources.
nano ./src/Demo.WebApi/Program.cs
private static IHost BuildWebHost(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(SetupConfiguration)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build();
}
private static void SetupConfiguration(HostBuilderContext context,
IConfigurationBuilder builder)
{
}
After configuration has been initialized, the next step is to create one or more application settings. Add the following example settings class to the Demo.App project as follows:
nano ./src/Components/Demo.App/ServiceSettings.cs
using NetFusion.Settings;
namespace Demo.App
{
[ConfigurationSection("microservice:database")]
public class ServiceSettings : IAppSettings
{
public ServiceSettings()
{
Ports = new int[] {};
}
public string Name { get; set; }
public string Url { get; set; }
public int[] Ports { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
Add the following JSON to the appsettings.json file contained within the Demo.WebApi project:
nano ./src/Demo.WebApi/appsettings.json
{
"microservice": {
"database": {
"name": "Example Database",
"url": "https://some/url/to/db",
"ports": [8080, 8081],
"username": "TestUser",
"password": "GSDA%%NH@@@"
}
}
}
Next, create a Web API controller injecting the settings class and return it from an action method:
nano ./src/Demo.WebApi/Controllers/SettingsController.cs
using Demo.App;
using Microsoft.AspNetCore.Mvc;
namespace Demo.WebApi.Controllers
{
[Route("api/settings")]
[ApiController]
public class SettingsController : ControllerBase
{
private readonly ServiceSettings _settings;
public SettingsController(ServiceSettings settings)
{
_settings = settings;
}
[HttpGet]
public IActionResult GetSettings()
{
return Ok(_settings);
}
}
}
Test the controller by invoking the following URL:
cd ./src/Demo.WebApi/
dotnet run
The following shows the results:
Note the following about this example:
- The settings class is just a simple class definition with the ConfigurationSection attribute specified.
- The attribute indicates the section path from which the settings should be loaded.
- The application settings can be injected directly into dependent components.
- If the ConfigurationSection attribute is not specified, a warning is written to the log.
When a setting class is injected into a dependent component, the setting class's state is validated. Since setting classes are simple structures, the default validation provided by NetFusion is used (Microsoft Data Annotations based). For more information on validation, refer to the NetFusion validation section. Update the above settings class to include the following validations:
cd ../..
nano ./src/Components/Demo.App/ServiceSettings.cs
using System.Linq;
using NetFusion.Base.Validation;
using NetFusion.Settings;
namespace Demo.App
{
[ConfigurationSection("microservice:database")]
public class ServiceSettings : IAppSettings,
IValidatableType
{
public ServiceSettings()
{
Ports = new int[] {};
}
public string Name { get; set; }
public string Url { get; set; }
public int[] Ports { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public void Validate(IObjectValidator validator)
{
validator.Verify(!string.IsNullOrWhiteSpace(Name), "Name not specified.");
validator.Verify(!string.IsNullOrWhiteSpace(Password), "Password not specified.");
validator.Verify(Ports.All(p => p > 8000), "All port must be greater than 8000.");
}
}
}
Next, update the appsetting.json file to include invalid data and re-execute the above two URL to test the validation.
nano ./src/Demo.WebApi/appsettings.json
{
"microservice": {
"database": {
"name": "",
"url": "https://some/url/to/db",
"ports": [8080, 5000],
"username": "TestUser",
"password": "GSDA%%NH@@@"
}
}
}
cd ./src/Demo.WebApi/
dotnet run
Execute the URL again and the following log message should be written:
-
Templates
-
Resources
-
Bootstrapping
-
Modules Details
-
Settings
-
Validation
-
Monitoring
- Setup
- Commands
- Queries
- Domain Events
- Message Logs
- Message Publishers
- Message Enrichers
- Message Filters
-
Azure Service Bus
-
RabbitMQ
-
Redis
-
MongoDB