-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeBaseInfrastructureServiceCollectionExtensions.cs
More file actions
58 lines (53 loc) · 3.22 KB
/
Copy pathKnowledgeBaseInfrastructureServiceCollectionExtensions.cs
File metadata and controls
58 lines (53 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using BuildingBlocks.Infrastructure.Persistence;
using KnowledgeBase.Application.Authorization;
using KnowledgeBase.Application.Entries;
using KnowledgeBase.Application.Settings;
using KnowledgeBase.Infrastructure.Configuration;
using KnowledgeBase.Infrastructure.Outbox;
using KnowledgeBase.Infrastructure.Persistence;
using KnowledgeBase.Infrastructure.Queries;
using KnowledgeBase.PublicContracts.Queries;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace KnowledgeBase.Infrastructure;
public static class KnowledgeBaseInfrastructureServiceCollectionExtensions
{
public static IServiceCollection AddKnowledgeBaseInfrastructure(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.AddOptions<KnowledgeBaseInfrastructureOptions>()
.Configure<IConfiguration>(static (options, configuration) =>
{
configuration.GetSection(KnowledgeBaseInfrastructureOptions.SectionName).Bind(options);
})
.Validate(
static options => !string.IsNullOrWhiteSpace(options.Settings.PublicExperienceTitle),
"KnowledgeBase settings require a public experience title.")
.Validate(
static options => !string.IsNullOrWhiteSpace(options.Settings.PublicExperienceBlurb),
"KnowledgeBase settings require a public experience blurb.")
.Validate(
static options => !string.IsNullOrWhiteSpace(options.Settings.SearchPlaceholder),
"KnowledgeBase settings require a search placeholder.")
.Validate(
static options => options.Settings.ManagementPreviewLimit >= KnowledgeBaseModuleSettingsDefaults.MinManagementPreviewLimit
&& options.Settings.ManagementPreviewLimit <= KnowledgeBaseModuleSettingsDefaults.MaxManagementPreviewLimit,
$"KnowledgeBase settings preview limit must be between {KnowledgeBaseModuleSettingsDefaults.MinManagementPreviewLimit} and {KnowledgeBaseModuleSettingsDefaults.MaxManagementPreviewLimit}.")
.ValidateOnStart();
services.TryAddSingleton<IKnowledgeBaseStore>(static serviceProvider =>
{
var dataSourceResolver = serviceProvider.GetRequiredService<IPostgresDataSourceResolver>();
var clock = serviceProvider.GetRequiredService<BuildingBlocks.Domain.Time.IClock>();
var options = serviceProvider.GetRequiredService<IOptions<KnowledgeBaseInfrastructureOptions>>().Value;
return new PostgresKnowledgeBaseStore(dataSourceResolver, clock, options);
});
services.TryAddSingleton<IKnowledgeBaseSettingsStore>(static provider =>
(IKnowledgeBaseSettingsStore)provider.GetRequiredService<IKnowledgeBaseStore>());
services.TryAddSingleton<IKnowledgeBasePublishedEntryQueryService, KnowledgeBasePublishedEntryQueryService>();
services.AddSingleton<IDatabaseMigration>(static provider => (IDatabaseMigration)provider.GetRequiredService<IKnowledgeBaseStore>());
services.AddKnowledgeBaseOutbox();
return services;
}
}