mongo-extensions icon indicating copy to clipboard operation
mongo-extensions copied to clipboard

Add IMongoClient constructor support to MongoDbContext for .NET Aspire integration

Open Copilot opened this issue 7 months ago • 1 comments

This PR adds support for initializing MongoDbContext directly from an IMongoClient instance, enabling seamless integration with .NET Aspire's MongoDB integration where IMongoClient is already registered in the dependency injection container.

Problem

When using .NET Aspire with MongoDB, the Aspire.MongoDB.Driver.v3 package registers an IMongoClient in the DI container. However, the existing MongoDbContext only provided constructors that accept MongoOptions, making it difficult to leverage the pre-configured client without workarounds.

Solution

Added new constructor overloads to MongoDbContext:

public MongoDbContext(IMongoClient mongoClient, string databaseName)
public MongoDbContext(IMongoClient mongoClient, string databaseName, bool enableAutoInitialize)

Usage Example

// Define your context
public class MyDbContext : MongoDbContext
{
    public MyDbContext(IMongoClient mongoClient, string databaseName) 
        : base(mongoClient, databaseName) { }
    
    protected override void OnConfiguring(IMongoDatabaseBuilder builder)
    {
        builder.RegisterDefaultConventionPack();
    }
}

// Register in DI with Aspire's IMongoClient
services.AddScoped<MyDbContext>(provider =>
{
    var mongoClient = provider.GetRequiredService<IMongoClient>();
    return new MyDbContext(mongoClient, "mydatabase");
});

Implementation Details

  • Backward Compatibility: All existing constructors and functionality remain unchanged
  • Dual Initialization Paths: Extended MongoDatabaseBuilder to support both MongoOptions and IMongoClient initialization
  • Interface Compliance: New constructors maintain full compatibility with IMongoDbContext interface
  • Configuration Handling: Client configuration methods are safely ignored when using pre-existing IMongoClient

Testing

  • ✅ All existing tests continue to pass (67 → 74 passed tests)
  • ✅ Added 7 comprehensive test cases covering new functionality
  • ✅ Manual testing validates real-world Aspire scenarios
  • ✅ Edge case testing ensures robust behavior

Benefits

  • Cleaner DI Integration: No need to manually create or configure MongoDB clients outside DI
  • Consistency: Matches patterns from other database providers in Aspire ecosystem
  • Improved Testability: Easier to mock IMongoClient for unit tests
  • Zero Breaking Changes: Fully backward compatible with existing code

Fixes #103.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Jul 16 '25 09:07 Copilot

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

swisslife-bot avatar Jul 16 '25 09:07 swisslife-bot