Kentico / xperience-algolia

Enables the creation of Algolia search indexes and the indexing of Xperience content tree pages using a code-first approach.
https://www.kentico.com
MIT License
1 stars 3 forks source link

Feature: Register IOptions<AlgoliaOptions> for DI #25

Closed kentico-ericd closed 2 years ago

kentico-ericd commented 2 years ago

Motivation

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

How to test

  1. Setup app using this library
  2. Do not provide any settings values
  3. Inject IOptions<AlgoliaOptions> into user-code (Controller/View Component/View) and verify default settings are available
  4. Supply settings values
  5. Inject IOptions<AlgoliaOptions> into user-code (Controller/View Component/View) and verify custom settings are available