Skip to content

Setup Guide

Aryeh Citron edited this page May 12, 2026 · 2 revisions

Setup Guide

Pattern 1: Singleton Client + Singleton Collection

The most common pattern. Replaces all MongoDB interface registrations at once.

// Production registration (Startup.cs / Program.cs)
services.AddSingleton<IMongoClient>(new MongoClient(connectionString));
services.AddSingleton(sp => sp.GetRequiredService<IMongoClient>().GetDatabase("mydb"));
services.AddSingleton(sp => sp.GetRequiredService<IMongoDatabase>().GetCollection<Order>("orders"));

// Test override
services.UseInMemoryMongoDB(options =>
{
    options.DatabaseName = "mydb";
    options.AddCollection<Order>("orders");
});

Pattern 2: Typed IMongoClient Subclasses

If your DI registers a typed client (e.g., IOrdersMongoClient : IMongoClient):

// Register specific collections for specific typed clients
services.UseInMemoryMongoDB(options =>
{
    options.DatabaseName = "ordersDb";
    options.AddCollection<Order>("orders");
});

Pattern 3: Singleton Client, Repos Call GetCollection()

When repositories resolve collections via client.GetDatabase().GetCollection<T>():

services.UseInMemoryMongoDB(options =>
{
    options.DatabaseName = "mydb";
    options.AddCollection<Order>("orders");
    options.AddCollection<Customer>("customers");
});

Repositories that call client.GetDatabase("mydb").GetCollection<Order>("orders") will get the in-memory collection automatically.

Pattern 4: Collection-Only Replacement

When you only need to replace IMongoCollection<T> registrations (not the client or database):

services.UseInMemoryMongoCollections(options =>
{
    options.DatabaseName = "mydb";
    options.AddCollection<Order>("orders");
});

Pattern 5: Custom Factory Interface + InMemoryMongoClient

For applications with a custom factory interface:

// Your production factory
public interface IMongoFactory
{
    IMongoCollection<T> GetCollection<T>(string name);
}

// Test implementation
public class InMemoryMongoFactory : IMongoFactory
{
    private readonly InMemoryMongoDatabase _db;

    public InMemoryMongoFactory()
    {
        var client = new InMemoryMongoClient();
        _db = (InMemoryMongoDatabase)client.GetDatabase("test");
    }

    public IMongoCollection<T> GetCollection<T>(string name)
        => _db.GetCollection<T>(name);
}

// Register in tests
services.AddSingleton<IMongoFactory>(new InMemoryMongoFactory());

Direct Instantiation

For the most control, instantiate directly:

var client = new InMemoryMongoClient();
var database = client.GetDatabase("mydb");
var collection = database.GetCollection<Order>("orders");

Or use the builder for multi-database setups:

var mongo = InMemoryMongo.Builder()
    .AddDatabase("db1", db =>
    {
        db.AddCollection<Order>("orders");
        db.AddCollection<Customer>("customers");
    })
    .AddDatabase("db2", db =>
    {
        db.AddCollection<Product>("products");
    })
    .Build();

Configuration Options

InMemoryMongoOptions

Property Type Description
DatabaseName string Database name for registrations
OnClientCreated Action<InMemoryMongoClient> Callback after client creation
OnDatabaseCreated Action<InMemoryMongoDatabase> Callback after database creation
AddCollection<T>(name) method Register a collection

InMemoryCollectionOptions

Property Type Description
DatabaseName string Database name for registrations
AddCollection<T>(name) method Register a collection

See Also

Clone this wiki locally