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

loresoft / MongoDB.Abstracts / 13142802022

04 Feb 2025 06:52PM UTC coverage: 76.38% (-1.1%) from 77.451%
13142802022

push

github

pwelter34
Update ServiceCollectionExtensions.cs

38 of 76 branches covered (50.0%)

Branch coverage included in aggregate %.

15 of 19 new or added lines in 1 file covered. (78.95%)

211 of 250 relevant lines covered (84.4%)

7.44 hits per line

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

69.32
/src/MongoDB.Abstracts/ServiceCollectionExtensions.cs
1
// Ignore Spelling: Mongo
2

3
using Microsoft.Extensions.Configuration;
4
using Microsoft.Extensions.DependencyInjection;
5
using Microsoft.Extensions.DependencyInjection.Extensions;
6

7
using MongoDB.Driver;
8

9
namespace MongoDB.Abstracts;
10

11
/// <summary>
12
/// Extension methods for <see cref="IServiceCollection"/>
13
/// </summary>
14
public static class ServiceCollectionExtensions
15
{
16
    /// <summary>
17
    /// Adds the MongoDB services with the specified connection string.
18
    /// </summary>
19
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
20
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
21
    /// <returns>
22
    /// The same service collection so that multiple calls can be chained.
23
    /// </returns>
24
    public static IServiceCollection AddMongoRepository(this IServiceCollection services, string nameOrConnectionString)
25
    {
26
        if (services is null)
1!
27
            throw new ArgumentNullException(nameof(services));
×
28

29
        if (nameOrConnectionString is null)
1!
30
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
31

32

33
        services.AddMongoDatabase(nameOrConnectionString);
1✔
34

35
        services.TryAddSingleton(typeof(IMongoEntityQuery<>), typeof(MongoEntityQuery<>));
1✔
36
        services.TryAddSingleton(typeof(IMongoEntityRepository<>), typeof(MongoEntityRepository<>));
1✔
37

38
        return services;
1✔
39
    }
40

41
    /// <summary>
42
    /// Adds the MongoDB services with the specified connection string using the <typeparamref name="TDiscriminator"/> to support typed registration
43
    /// </summary>
44
    /// <typeparam name="TDiscriminator">The type of the connection discriminator.</typeparam>
45
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
46
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
47
    /// <returns>
48
    /// The same service collection so that multiple calls can be chained.
49
    /// </returns>
50
    public static IServiceCollection AddMongoRepository<TDiscriminator>(this IServiceCollection services, string nameOrConnectionString)
51
    {
52
        if (services is null)
1!
53
            throw new ArgumentNullException(nameof(services));
×
54

55
        if (nameOrConnectionString is null)
1!
56
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
57

58

59
        services.AddMongoDatabase<TDiscriminator>(nameOrConnectionString);
1✔
60

61
        services.TryAddSingleton(typeof(IMongoEntityQuery<,>), typeof(MongoEntityQuery<,>));
1✔
62
        services.TryAddSingleton(typeof(IMongoEntityRepository<,>), typeof(MongoEntityRepository<,>));
1✔
63

64
        return services;
1✔
65
    }
66

67

68
    /// <summary>
69
    /// Adds the MongoDB database services with the specified connection string and service key.
70
    /// </summary>
71
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
72
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
73
    /// <returns>
74
    /// The same service collection so that multiple calls can be chained.
75
    /// </returns>
76
    public static IServiceCollection AddMongoDatabase(this IServiceCollection services, string nameOrConnectionString)
77
    {
78
        if (services is null)
1!
NEW
79
            throw new ArgumentNullException(nameof(services));
×
80

81
        if (nameOrConnectionString is null)
1!
NEW
82
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
83

84
        services.TryAddSingleton(sp =>
1✔
85
        {
1✔
86
            var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
87
            return MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
88
        });
1✔
89

90
        return services;
1✔
91
    }
92

93
    /// <summary>
94
    /// Adds the MongoDB database services with the specified connection string using the <typeparamref name="TDiscriminator"/> to support typed registration
95
    /// </summary>
96
    /// <typeparam name="TDiscriminator">The type of the connection discriminator.</typeparam>
97
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
98
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
99
    /// <returns>
100
    /// The same service collection so that multiple calls can be chained.
101
    /// </returns>
102
    public static IServiceCollection AddMongoDatabase<TDiscriminator>(this IServiceCollection services, string nameOrConnectionString)
103
    {
104
        if (services is null)
1!
NEW
105
            throw new ArgumentNullException(nameof(services));
×
106

107
        if (nameOrConnectionString is null)
1!
NEW
108
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
109

110

111
        services.TryAddSingleton(sp =>
1✔
112
        {
1✔
113
            var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
114
            var database = MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
115

1✔
116
            return new MongoDiscriminator<TDiscriminator>(database);
1✔
117
        });
1✔
118

119
        return services;
1✔
120
    }
121

122
    /// <summary>
123
    /// Adds the MongoDB database services with the specified connection string and service key.
124
    /// </summary>
125
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
126
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
127
    /// <param name="serviceKey">The service key.</param>
128
    /// <returns>
129
    /// The same service collection so that multiple calls can be chained.
130
    /// </returns>
131
    public static IServiceCollection AddMongoDatabase(this IServiceCollection services, string nameOrConnectionString, object? serviceKey)
132
    {
133
        if (services is null)
1!
134
            throw new ArgumentNullException(nameof(services));
×
135

136
        if (nameOrConnectionString is null)
1!
137
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
138

139
        services.TryAddKeyedSingleton(
1✔
140
            serviceKey: serviceKey,
1✔
141
            implementationFactory: (sp, key) =>
1✔
142
            {
1✔
143
                var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
144
                return MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
145
            }
1✔
146
        );
1✔
147

148
        return services;
1✔
149
    }
150

151

152
    private static string ResolveConnectionString(IServiceProvider serviceProvider, string nameOrConnectionString)
153
    {
154
        var isConnectionString = nameOrConnectionString.IndexOfAny([';', '=', ':', '/']) > 0;
3✔
155
        if (isConnectionString)
3✔
156
            return nameOrConnectionString;
2✔
157

158
        var configuration = serviceProvider.GetRequiredService<IConfiguration>();
1✔
159

160
        // first try connection strings section
161
        var connectionString = configuration.GetConnectionString(nameOrConnectionString);
1✔
162
        if (!string.IsNullOrEmpty(connectionString))
1!
163
            return connectionString;
1✔
164

165
        // next try root collection
166
        connectionString = configuration[nameOrConnectionString];
×
167
        if (!string.IsNullOrEmpty(connectionString))
×
168
            return connectionString;
×
169

170
        return nameOrConnectionString;
×
171
    }
172
}
173

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