• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Avanade / NTangle / 11788290792

12 Nov 2024 12:19AM UTC coverage: 73.895% (+9.4%) from 64.469%
11788290792

Pull #52

github

web-flow
Merge 603ddbb9d into 5cd46db5b
Pull Request #52: v3.0.0

256 of 372 branches covered (68.82%)

Branch coverage included in aggregate %.

830 of 996 new or added lines in 40 files covered. (83.33%)

48 existing lines in 1 file now uncovered.

1199 of 1597 relevant lines covered (75.08%)

28.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/NTangle/SidecarServiceCollectionExtensions.cs
1
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/NTangle
2

3
using CoreEx.Database.HealthChecks;
4
using Microsoft.Extensions.Diagnostics.HealthChecks;
5
using System;
6
using NTangle.Data;
7

8
namespace Microsoft.Extensions.DependencyInjection
9
{
10
    /// <summary>
11
    /// Provides <see cref="IServiceCollection"/> extension methods.
12
    /// </summary>
13
    public static class DatabaseServiceCollectionExtensions
14
    {
15
        /// <summary>
16
        /// Adds an <see cref="ISidecarDatabase"/> as a scoped service.
17
        /// </summary>
18
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
19
        /// <param name="create">The function to create the <see cref="ISidecarDatabase"/> instance.</param>
20
        /// <param name="healthCheck">Indicates whether a corresponding <see cref="DatabaseHealthCheck{TDatabase}"/> should be configured.</param>
21
        /// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
22
        public static IServiceCollection AddSidecarDatabase(this IServiceCollection services, Func<IServiceProvider, ISidecarDatabase> create, bool healthCheck = true)
NEW
23
        {
×
NEW
24
            services.AddScoped(sp => create(sp) ?? throw new InvalidOperationException($"An {nameof(ISidecarDatabase)} instance must be instantiated."));
×
NEW
25
            return AddHealthCheck(services, healthCheck, null);
×
NEW
26
        }
×
27

28
        /// <summary>
29
        /// Adds an <see cref="ISidecarDatabase"/> as a scoped service including a corresponding health check.
30
        /// </summary>
31
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
32
        /// <param name="create">The function to create the <see cref="ISidecarDatabase"/> instance.</param>
33
        /// <param name="healthCheckName">The health check name; defaults to '<c>sidecar-db</c>'.</param>
34
        /// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
35
        public static IServiceCollection AddSidecarDatabase(this IServiceCollection services, Func<IServiceProvider, ISidecarDatabase> create, string? healthCheckName)
NEW
36
        {
×
NEW
37
            services.AddScoped(sp => create(sp) ?? throw new InvalidOperationException($"An {nameof(ISidecarDatabase)} instance must be instantiated."));
×
NEW
38
            return AddHealthCheck(services, true, healthCheckName);
×
NEW
39
        }
×
40

41
        /// <summary>
42
        /// Adds an <see cref="ISidecarDatabase"/> as a scoped service.
43
        /// </summary>
44
        /// <typeparam name="TDb">The <see cref="ISidecarDatabase"/> <see cref="Type"/>.</typeparam>
45
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
46
        /// <param name="healthCheck">Indicates whether a corresponding <see cref="DatabaseHealthCheck{TDatabase}"/> should be configured.</param>
47
        /// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
48
        public static IServiceCollection AddSidecarDatabase<TDb>(this IServiceCollection services, bool healthCheck = true) where TDb : class, ISidecarDatabase
NEW
49
        {
×
NEW
50
            services.AddScoped<ISidecarDatabase, TDb>();
×
NEW
51
            return AddHealthCheck(services, healthCheck, null);
×
NEW
52
        }
×
53

54
        /// <summary>
55
        /// Adds an <see cref="ISidecarDatabase"/> as a scoped service including a corresponding health check.
56
        /// </summary>
57
        /// <typeparam name="TDb">The <see cref="ISidecarDatabase"/> <see cref="Type"/>.</typeparam>
58
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
59
        /// <param name="healthCheckName">The health check name; defaults to '<c>database</c>'.</param>
60
        /// <returns>The <see cref="IServiceCollection"/> to support fluent-style method-chaining.</returns>
61
        public static IServiceCollection AddSidecarDatabase<TDb>(this IServiceCollection services, string? healthCheckName) where TDb : class, ISidecarDatabase
NEW
62
        {
×
NEW
63
            services.AddScoped<ISidecarDatabase, TDb>();
×
NEW
64
            return AddHealthCheck(services, true, healthCheckName);
×
NEW
65
        }
×
66

67
        /// <summary>
68
        /// Adds the <see cref="DatabaseHealthCheck{TDatabase}"/> where configured to do so.
69
        /// </summary>
70
        private static IServiceCollection AddHealthCheck(this IServiceCollection services, bool healthCheck, string? healthCheckName)
NEW
71
        {
×
NEW
72
            if (healthCheck)
×
NEW
73
                services.AddHealthChecks().AddTypeActivatedCheck<DatabaseHealthCheck<ISidecarDatabase>>(healthCheckName ?? "sidecar-db", HealthStatus.Unhealthy, tags: default!, timeout: TimeSpan.FromSeconds(30));
×
74

NEW
75
            return services;
×
NEW
76
        }
×
77
    }
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc