-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCoreExReferenceDataExtensions.DependencyInjection.cs
More file actions
62 lines (55 loc) · 4.88 KB
/
Copy pathCoreExReferenceDataExtensions.DependencyInjection.cs
File metadata and controls
62 lines (55 loc) · 4.88 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
59
60
61
62
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace Microsoft.Extensions.DependencyInjection;
#pragma warning restore IDE0130 // Namespace does not match folder structure
/// <summary>
/// Provides standard extensions.
/// </summary>
public static partial class CoreExReferenceDataExtensions
{
/// <summary>
/// Adds the <see cref="ReferenceDataOrchestrator"/> created by the <paramref name="orchestratorFactory"/> as a singleton service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="orchestratorFactory">The function to create the <see cref="ReferenceDataOrchestrator"/>.</param>
/// <param name="healthCheck">Indicates whether a corresponding <see cref="ReferenceDataOrchestratorHealthCheck"/> should be configured.</param>
/// <param name="healthCheckName">The health check name; defaults to '<c>reference-data-orchestrator</c>'.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
/// <remarks>Also, registers the <see cref="ReferenceDataHybridCache"/> as the required <see cref="IReferenceDataCache"/> scoped service.</remarks>
public static IServiceCollection AddReferenceDataOrchestrator(this IServiceCollection services, Func<IServiceProvider, ReferenceDataOrchestrator> orchestratorFactory, bool healthCheck = true, string? healthCheckName = "reference-data-orchestrator")
{
services.ThrowIfNull().AddSingleton(sp =>
{
var orchestrator = orchestratorFactory(sp);
if (orchestrator == null)
throw new InvalidOperationException("The ReferenceDataOrchestrator factory returned a null value.");
if (!orchestrator.HasRegisteredQuery)
orchestrator.RegisterQuery(ReferenceDataQuery.Default);
return orchestrator;
});
if (healthCheck)
services.AddHealthChecks().AddTypeActivatedCheck<ReferenceDataOrchestratorHealthCheck>(healthCheckName ?? "reference-data-orchestrator", null, tags: HealthCheckTags.StartUpAndReadyOnly);
services.TryAddScoped<IReferenceDataCache>(sp => new ReferenceDataHybridCache(sp.GetService<IHybridCache>() ?? ActivatorUtilities.GetServiceOrCreateInstance<MemoryOnlyHybridCache>(sp)));
return services;
}
/// <summary>
/// Adds the <see cref="ReferenceDataOrchestrator"/> using an <see cref="IReferenceDataCache"/> as a singleton service automatically registering the <see cref="IReferenceDataProvider"/> (see <see cref="ReferenceDataOrchestrator.Register"/>).
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="healthCheck">Indicates whether a corresponding <see cref="ReferenceDataOrchestratorHealthCheck"/> should be configured.</param>
/// <param name="healthCheckName">The health check name; defaults to '<c>reference-data-orchestrator</c>'.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
/// <remarks>Where an <see cref="IReferenceDataCache"/> has not been registered then the <see cref="ReferenceDataHybridCache"/> will be used by default.</remarks>
public static IServiceCollection AddReferenceDataOrchestrator(this IServiceCollection services, bool healthCheck = true, string? healthCheckName = "reference-data-orchestrator")
=> AddReferenceDataOrchestrator(services, sp => new ReferenceDataOrchestrator(sp, sp.GetRequiredService<ILogger<ReferenceDataOrchestrator>>()).Register(), healthCheck, healthCheckName);
/// <summary>
/// Adds the <see cref="ReferenceDataOrchestrator"/> using an <see cref="IReferenceDataCache"/> as a singleton service automatically registering the specified <typeparamref name="TProvider"/> (see <see cref="ReferenceDataOrchestrator.Register"/>).
/// </summary>
/// <typeparam name="TProvider">The <see cref="IReferenceDataProvider"/> to register.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="healthCheck">Indicates whether a corresponding <see cref="ReferenceDataOrchestratorHealthCheck"/> should be configured.</param>
/// <param name="healthCheckName">The health check name; defaults to '<c>reference-data-orchestrator</c>'.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
/// <remarks>Where an <see cref="IReferenceDataCache"/> has not been registered then the <see cref="ReferenceDataHybridCache"/> will be used by default.</remarks>
public static IServiceCollection AddReferenceDataOrchestrator<TProvider>(this IServiceCollection services, bool healthCheck = true, string? healthCheckName = "reference-data-orchestrator") where TProvider : IReferenceDataProvider
=> AddReferenceDataOrchestrator(services, sp => new ReferenceDataOrchestrator(sp, sp.GetRequiredService<ILogger<ReferenceDataOrchestrator>>()).Register<TProvider>(), healthCheck, healthCheckName);
}