Skip to content

Commit

Permalink
Update ServiceCollectionExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Feb 4, 2025
1 parent 0520ecf commit fa246dc
Showing 1 changed file with 54 additions and 9 deletions.
63 changes: 54 additions & 9 deletions src/MongoDB.Abstracts/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public static IServiceCollection AddMongoRepository(this IServiceCollection serv
throw new ArgumentNullException(nameof(nameOrConnectionString));


services.TryAddSingleton(sp =>
{
var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
return MongoFactory.GetDatabaseFromConnectionString(connectionString);
});
services.AddMongoDatabase(nameOrConnectionString);

services.TryAddSingleton(typeof(IMongoEntityQuery<>), typeof(MongoEntityQuery<>));
services.TryAddSingleton(typeof(IMongoEntityRepository<>), typeof(MongoEntityRepository<>));
Expand All @@ -60,6 +56,58 @@ public static IServiceCollection AddMongoRepository<TDiscriminator>(this IServic
throw new ArgumentNullException(nameof(nameOrConnectionString));


services.AddMongoDatabase<TDiscriminator>(nameOrConnectionString);

services.TryAddSingleton(typeof(IMongoEntityQuery<,>), typeof(MongoEntityQuery<,>));
services.TryAddSingleton(typeof(IMongoEntityRepository<,>), typeof(MongoEntityRepository<,>));

return services;
}


/// <summary>
/// Adds the MongoDB database services with the specified connection string and service key.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
public static IServiceCollection AddMongoDatabase(this IServiceCollection services, string nameOrConnectionString)
{
if (services is null)
throw new ArgumentNullException(nameof(services));

if (nameOrConnectionString is null)
throw new ArgumentNullException(nameof(nameOrConnectionString));

services.TryAddSingleton(sp =>
{
var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
return MongoFactory.GetDatabaseFromConnectionString(connectionString);
});

return services;
}

/// <summary>
/// Adds the MongoDB database services with the specified connection string using the <typeparamref name="TDiscriminator"/> to support typed registration
/// </summary>
/// <typeparam name="TDiscriminator">The type of the connection discriminator.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
public static IServiceCollection AddMongoDatabase<TDiscriminator>(this IServiceCollection services, string nameOrConnectionString)
{
if (services is null)
throw new ArgumentNullException(nameof(services));

if (nameOrConnectionString is null)
throw new ArgumentNullException(nameof(nameOrConnectionString));


services.TryAddSingleton(sp =>
{
var connectionString = ResolveConnectionString(sp, nameOrConnectionString);
Expand All @@ -68,9 +116,6 @@ public static IServiceCollection AddMongoRepository<TDiscriminator>(this IServic
return new MongoDiscriminator<TDiscriminator>(database);
});

services.TryAddSingleton(typeof(IMongoEntityQuery<,>), typeof(MongoEntityQuery<,>));
services.TryAddSingleton(typeof(IMongoEntityRepository<,>), typeof(MongoEntityRepository<,>));

return services;
}

Expand Down Expand Up @@ -103,6 +148,7 @@ public static IServiceCollection AddMongoDatabase(this IServiceCollection servic
return services;
}


private static string ResolveConnectionString(IServiceProvider serviceProvider, string nameOrConnectionString)
{
var isConnectionString = nameOrConnectionString.IndexOfAny([';', '=', ':', '/']) > 0;
Expand All @@ -123,6 +169,5 @@ private static string ResolveConnectionString(IServiceProvider serviceProvider,

return nameOrConnectionString;
}

}

0 comments on commit fa246dc

Please sign in to comment.