The current recommended approach for getting access to the Algolia configuration in the ASP.NET Core application from app settings is the following:
@inject IConfiguration configuration
@{
var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>();
}
This block of code needs to be repeated wherever these settings are needed. It's also not recommended to inject IConfiguration across an application because it makes it difficult to determine what parts of configuration are being used by an application.
However, .NET already provides patterns for validating configuration and injecting it across an application with IOptions and PostConfigure.
This PR makes the AlgoliaOptions available through IOptions<AlgoliaOptions> and also ensures the options get populated with default values before they are used.
There are no breaking changes since developers can still use IConfiguration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>(); if they want.
Checklist
[X] Code follows coding conventions held in this repo
[ ] Automated tests have been added
[X] Tests are passing
[X] Docs have been updated (if applicable)
[X] Temporary settings (e.g. variables used during development and testing) have been reverted to defaults
How to test
Setup app using this library
Do not provide any settings values
Inject IOptions<AlgoliaOptions> into user-code (Controller/View Component/View) and verify default settings are available
Supply settings values
Inject IOptions<AlgoliaOptions> into user-code (Controller/View Component/View) and verify custom settings are available
Motivation
The current recommended approach for getting access to the Algolia configuration in the ASP.NET Core application from app settings is the following:
This block of code needs to be repeated wherever these settings are needed. It's also not recommended to inject
IConfiguration
across an application because it makes it difficult to determine what parts of configuration are being used by an application.However, .NET already provides patterns for validating configuration and injecting it across an application with IOptions and PostConfigure .
This PR makes the
AlgoliaOptions
available throughIOptions<AlgoliaOptions>
and also ensures the options get populated with default values before they are used.There are no breaking changes since developers can still use
IConfiguration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>();
if they want.Checklist
How to test
IOptions<AlgoliaOptions>
into user-code (Controller/View Component/View) and verify default settings are availableIOptions<AlgoliaOptions>
into user-code (Controller/View Component/View) and verify custom settings are available