TestingCommons.MongoDb
1.0.3
dotnet add package TestingCommons.MongoDb --version 1.0.3
NuGet\Install-Package TestingCommons.MongoDb -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TestingCommons.MongoDb" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TestingCommons.MongoDb" Version="1.0.3" />
<PackageReference Include="TestingCommons.MongoDb" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TestingCommons.MongoDb --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TestingCommons.MongoDb, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TestingCommons.MongoDb@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TestingCommons.MongoDb&version=1.0.3
#tool nuget:?package=TestingCommons.MongoDb&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
TestingCommons.MongoDb
The MongoDb project provides a robust base client for MongoDB operations in testing scenarios. It includes pre-configured serialization, transaction support, and comprehensive configuration options.
Key Components
MongoDbClientBase
- Abstract base class for MongoDB client implementations
- Pre-configured with standard BSON serialization conventions
- Supports TLS 1.2 connections and SSL settings
- Automatic configuration of camel case conventions and enum string representation
MongoDbConfiguration
- Comprehensive configuration class for MongoDB settings
- Connection string, database name, logging options
- Transaction support configuration
- Read preference modes and staleness settings
- Message consume delay configuration
Transaction Support
MongoDbTransactionOptions- Configuration for MongoDB transactions- Read/Write concern level management
- Support for distributed transactions when enabled
Serialization Provider
- Custom BSON serialization for .NET types
- Decimal serialization as BSON Decimal128
- DateTimeOffset serialization as string
- Proper handling of nullable types
Usage Examples
Basic Configuration
var mongoConfig = new MongoDbConfiguration
{
ConnectionString = "mongodb://localhost:27017",
Database = "TestDatabase",
EnableCommandTextLogging = true,
EnableMongoDbConnectionDiagnostics = true,
HasTransactionSupportEnabled = true
};
Creating a Custom Client
public class MyMongoClient : MongoDbClientBase
{
public MyMongoClient(MongoDbConfiguration config) : base(config)
{
}
public IMongoCollection<TDocument> GetCollection<TDocument>(string name)
{
return GetDatabase().GetCollection<TDocument>(name);
}
public async Task<List<TDocument>> FindAllAsync<TDocument>(string collectionName)
{
var collection = GetCollection<TDocument>(collectionName);
return await collection.Find(_ => true).ToListAsync();
}
}
// Usage
var client = new MyMongoClient(mongoConfig);
var users = await client.FindAllAsync<User>("users");
Transaction Configuration
var transactionOptions = new MongoDbTransactionOptions
{
ReadConcern = ReadConcernLevel.Majority,
WriteConcern = "majority" // Can be "majority", "1", "2", etc.
};
var config = new MongoDbConfiguration
{
ConnectionString = "mongodb://localhost:27017",
Database = "TestDatabase",
HasTransactionSupportEnabled = true,
TransactionOptions = transactionOptions
};
Working with Collections
public class DocumentService
{
private readonly IMongoDbClientBase _mongoClient;
public DocumentService(IMongoDbClientBase mongoClient)
{
_mongoClient = mongoClient;
}
public async Task<User> CreateUserAsync(User user)
{
var database = _mongoClient.GetDatabase();
var collection = database.GetCollection<User>("users");
await collection.InsertOneAsync(user);
return user;
}
public async Task<List<User>> GetActiveUsersAsync()
{
var database = _mongoClient.GetDatabase();
var collection = database.GetCollection<User>("users");
return await collection
.Find(u => u.IsActive)
.ToListAsync();
}
}
Key Features
- Pre-configured Serialization: Automatic setup of BSON conventions and serializers
- Transaction Support: Optional MongoDB transaction capabilities
- SSL/TLS Security: Built-in TLS 1.2 support for secure connections
- Flexible Configuration: Comprehensive options for different MongoDB setups
- Read Preferences: Support for primary/secondary read preferences
- Logging Integration: Optional command text logging and diagnostics
- Type Safety: Strong typing with generic collection operations
- Convention-based: Automatic camel case and enum string representation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- MongoDB.Driver (>= 3.4.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.