generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDatabaseServiceCollectionExtensions.cs
78 lines (71 loc) · 4.42 KB
/
DatabaseServiceCollectionExtensions.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Database;
using CoreEx.Database.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Provides <see cref="IServiceCollection"/> extension methods.
/// </summary>
public static class DatabaseServiceCollectionExtensions
{
/// <summary>
/// Adds an <see cref="IDatabase"/> as a scoped service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="create">The function to create the <see cref="IDatabase"/> instance.</param>
/// <param name="healthCheck">Indicates whether a corresponding <see cref="DatabaseHealthCheck{TDatabase}"/> should be configured.</param>
/// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
public static IServiceCollection AddDatabase(this IServiceCollection services, Func<IServiceProvider, IDatabase> create, bool healthCheck = true)
{
services.AddScoped(sp => create(sp) ?? throw new InvalidOperationException($"An {nameof(IDatabase)} instance must be instantiated."));
return AddHealthCheck(services, healthCheck, null);
}
/// <summary>
/// Adds an <see cref="IDatabase"/> as a scoped service including a corresponding health check.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="create">The function to create the <see cref="IDatabase"/> instance.</param>
/// <param name="healthCheckName">The health check name; defaults to '<c>database</c>'.</param>
/// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
public static IServiceCollection AddDatabase(this IServiceCollection services, Func<IServiceProvider, IDatabase> create, string? healthCheckName)
{
services.AddScoped(sp => create(sp) ?? throw new InvalidOperationException($"An {nameof(IDatabase)} instance must be instantiated."));
return AddHealthCheck(services, true, healthCheckName);
}
/// <summary>
/// Adds an <see cref="IDatabase"/> as a scoped service.
/// </summary>
/// <typeparam name="TDb">The <see cref="IDatabase"/> <see cref="Type"/>.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="healthCheck">Indicates whether a corresponding <see cref="DatabaseHealthCheck{TDatabase}"/> should be configured.</param>
/// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
public static IServiceCollection AddDatabase<TDb>(this IServiceCollection services, bool healthCheck = true) where TDb : class, IDatabase
{
services.AddScoped<IDatabase, TDb>();
return AddHealthCheck(services, healthCheck, null);
}
/// <summary>
/// Adds an <see cref="IDatabase"/> as a scoped service including a corresponding health check.
/// </summary>
/// <typeparam name="TDb">The <see cref="IDatabase"/> <see cref="Type"/>.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="healthCheckName">The health check name; defaults to '<c>database</c>'.</param>
/// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
public static IServiceCollection AddDatabase<TDb>(this IServiceCollection services, string? healthCheckName) where TDb : class, IDatabase
{
services.AddScoped<IDatabase, TDb>();
return AddHealthCheck(services, true, healthCheckName);
}
/// <summary>
/// Adds the <see cref="DatabaseHealthCheck{TDatabase}"/> where configured to do so.
/// </summary>
private static IServiceCollection AddHealthCheck(this IServiceCollection services, bool healthCheck, string? healthCheckName)
{
if (healthCheck)
services.AddHealthChecks().AddTypeActivatedCheck<DatabaseHealthCheck<IDatabase>>(healthCheckName ?? "database", HealthStatus.Unhealthy, tags: default!, timeout: TimeSpan.FromSeconds(30));
return services;
}
}
}