Skip to content

Commit

Permalink
Merge pull request #25 from Kentico/seangwright-refactor/configuration
Browse files Browse the repository at this point in the history
Feature: Register IOptions<AlgoliaOptions> for DI
  • Loading branch information
kentico-ericd authored Aug 9, 2022
2 parents c880963 + 3e723f7 commit 9f95163
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,11 @@ Algolia provides [autocomplete](https://www.algolia.com/doc/ui-libraries/autocom
3. Load the Algolia keys from `appsettings.json`:

```cshtml
@inject IConfiguration configuration
@using Microsoft.Extensions.Options
@inject IOptions<AlgoliaOptions> options
@{
var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>();
var algoliaOptions = options.Value;
}
```

Expand Down Expand Up @@ -1087,9 +1089,11 @@ endpoints.MapControllerRoute(
2. Create the _Index.cshtml_ view for your controller with the basic layout and stylesheet references. Load your Algolia settings for use later:
```cshtml
@inject IConfiguration configuration
@using Microsoft.Extensions.Options
@inject IOptions<AlgoliaOptions> options

@{
var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>();
var algoliaOptions = options.Value;
}

@section styles {
Expand Down
42 changes: 26 additions & 16 deletions src/AlgoliaStartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Algolia.Search.Clients;
using System;

using Algolia.Search.Clients;

using Kentico.Xperience.AlgoliaSearch.Models;
using Kentico.Xperience.AlgoliaSearch.Services;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using System;
using Microsoft.Extensions.Options;

namespace Kentico.Xperience.AlgoliaSearch
{
Expand All @@ -24,23 +25,32 @@ public static class AlgoliaStartupExtensions
/// <param name="register">The implementation of <see cref="IAlgoliaIndexRegister"/> to register.</param>
public static IServiceCollection AddAlgolia(this IServiceCollection services, IConfiguration configuration, IAlgoliaIndexRegister register)
{
var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get<AlgoliaOptions>();
if (String.IsNullOrEmpty(algoliaOptions.ApplicationId) || String.IsNullOrEmpty(algoliaOptions.ApiKey))
services.Configure<AlgoliaOptions>(configuration.GetSection(AlgoliaOptions.SECTION_NAME));
services.PostConfigure<AlgoliaOptions>(options =>
{
// Algolia configuration is not valid, but IEventLogService can't be resolved during startup.
// Set dummy values so that DI is not broken, but errors can be captured when attempting to use the client
algoliaOptions.ApplicationId = "NO_APP";
algoliaOptions.ApiKey = "NO_KEY";
}
if (String.IsNullOrEmpty(options.ApplicationId) || String.IsNullOrEmpty(options.ApiKey))
{
// Algolia configuration is not valid, but IEventLogService can't be resolved during startup.
// Set dummy values so that DI is not broken, but errors can be captured when attempting to use the client
options.ApplicationId = "NO_APP";
options.ApiKey = "NO_KEY";
}
});

var insightsClient = new InsightsClient(algoliaOptions.ApplicationId, algoliaOptions.ApiKey);
var searchClient = new SearchClient(algoliaOptions.ApplicationId, algoliaOptions.ApiKey);
return services
.AddSingleton<IInsightsClient>(s =>
{
var options = s.GetRequiredService<IOptions<AlgoliaOptions>>();

services.AddSingleton<IInsightsClient>(insightsClient);
services.AddSingleton<ISearchClient>(searchClient);
services.AddSingleton(register);
return new InsightsClient(options.Value.ApplicationId, options.Value.ApiKey);
})
.AddSingleton<ISearchClient>(s =>
{
var options = s.GetRequiredService<IOptions<AlgoliaOptions>>();

return services;
return new SearchClient(options.Value.ApplicationId, options.Value.ApiKey);
})
.AddSingleton(register);
}
}
}
2 changes: 1 addition & 1 deletion src/Kentico.Xperience.AlgoliaSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<Title>Xperience Algolia Search</Title>
<PackageId>Kentico.Xperience.AlgoliaSearch</PackageId>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<Authors>Kentico Software</Authors>
<Company>Kentico Software</Company>
<PackageIcon>icon.png</PackageIcon>
Expand Down

0 comments on commit 9f95163

Please sign in to comment.