• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

loresoft / MongoDB.Abstracts / 13124235992

03 Feb 2025 10:36PM UTC coverage: 77.743% (+0.3%) from 77.474%
13124235992

push

github

pwelter34
add Discriminator  support, rename AddMongoDB to AddMongoRepository

35 of 70 branches covered (50.0%)

Branch coverage included in aggregate %.

20 of 22 new or added lines in 4 files covered. (90.91%)

213 of 249 relevant lines covered (85.54%)

7.21 hits per line

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

72.06
/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.TryAddSingleton(sp =>
1✔
34
        {
1✔
35
            var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
36
            return MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
37
        });
1✔
38

39
        services.TryAddSingleton(typeof(IMongoEntityQuery<>), typeof(MongoEntityQuery<>));
1✔
40
        services.TryAddSingleton(typeof(IMongoEntityRepository<>), typeof(MongoEntityRepository<>));
1✔
41

42
        return services;
1✔
43
    }
44

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

59
        if (nameOrConnectionString is null)
1!
NEW
60
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
61

62

63
        services.TryAddSingleton(sp =>
1✔
64
        {
1✔
65
            var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
66
            var database = MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
67

1✔
68
            return new MongoDiscriminator<TDiscriminator>(database);
1✔
69
        });
1✔
70

71
        services.TryAddSingleton(typeof(IMongoEntityQuery<,>), typeof(MongoEntityQuery<,>));
1✔
72
        services.TryAddSingleton(typeof(IMongoEntityRepository<,>), typeof(MongoEntityRepository<,>));
1✔
73

74
        return services;
1✔
75
    }
76

77
    /// <summary>
78
    /// Adds the MongoDB database services with the specified connection string and service key.
79
    /// </summary>
80
    /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
81
    /// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
82
    /// <param name="serviceKey">The service key.</param>
83
    /// <returns>
84
    /// The same service collection so that multiple calls can be chained.
85
    /// </returns>
86
    public static IServiceCollection AddMongoDatabase(this IServiceCollection services, string nameOrConnectionString, object? serviceKey)
87
    {
88
        if (services is null)
1!
89
            throw new ArgumentNullException(nameof(services));
×
90

91
        if (nameOrConnectionString is null)
1!
92
            throw new ArgumentNullException(nameof(nameOrConnectionString));
×
93

94
        services.TryAddKeyedSingleton(
1✔
95
            serviceKey: serviceKey,
1✔
96
            implementationFactory: (sp, key) =>
1✔
97
            {
1✔
98
                var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
1✔
99
                return MongoFactory.GetDatabaseFromConnectionString(connectionString);
1✔
100
            }
1✔
101
        );
1✔
102

103
        return services;
1✔
104
    }
105

106
    private static string ResolveConnectionString(IServiceProvider serviceProvider, string nameOrConnectionString)
107
    {
108
        var isConnectionString = nameOrConnectionString.IndexOfAny([';', '=', ':', '/']) > 0;
3✔
109
        if (isConnectionString)
3✔
110
            return nameOrConnectionString;
2✔
111

112
        var configuration = serviceProvider.GetRequiredService<IConfiguration>();
1✔
113

114
        // first try connection strings section
115
        var connectionString = configuration.GetConnectionString(nameOrConnectionString);
1✔
116
        if (!string.IsNullOrEmpty(connectionString))
1!
117
            return connectionString;
1✔
118

119
        // next try root collection
120
        connectionString = configuration[nameOrConnectionString];
×
121
        if (!string.IsNullOrEmpty(connectionString))
×
122
            return connectionString;
×
123

124
        return nameOrConnectionString;
×
125
    }
126

127
}
128

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

© 2025 Coveralls, Inc