Workleap.Extensions.Mongo.Ephemeral 1.5.2-preview.2

Prefix Reserved
This is a prerelease version of Workleap.Extensions.Mongo.Ephemeral.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Workleap.Extensions.Mongo.Ephemeral --version 1.5.2-preview.2                
NuGet\Install-Package Workleap.Extensions.Mongo.Ephemeral -Version 1.5.2-preview.2                
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="Workleap.Extensions.Mongo.Ephemeral" Version="1.5.2-preview.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Workleap.Extensions.Mongo.Ephemeral --version 1.5.2-preview.2                
#r "nuget: Workleap.Extensions.Mongo.Ephemeral, 1.5.2-preview.2"                
#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.
// Install Workleap.Extensions.Mongo.Ephemeral as a Cake Addin
#addin nuget:?package=Workleap.Extensions.Mongo.Ephemeral&version=1.5.2-preview.2&prerelease

// Install Workleap.Extensions.Mongo.Ephemeral as a Cake Tool
#tool nuget:?package=Workleap.Extensions.Mongo.Ephemeral&version=1.5.2-preview.2&prerelease                

Workleap.Extensions.Mongo

nuget build

Workleap.Extensions.Mongo is a convenient set of .NET libraries designed to enhance and streamline the MongoDB C# driver integration into your C# projects.

Value proposition and features overview

Integrating the MongoDB C# driver into your C# projects can often lead to several questions:

  • What's the optimal way to configure a MongoDB client from my app configuration? Should I use appsettings.json, environment variables, custom option classes, plain C# code?
  • How can I expose the MongoDB client to the rest of my code? Should I use dependency injection?
  • What are the best practices for configuring the MongoDB client? How should MongoDB C# driver static settings be handled?
  • If I want to support multiple MongoDB clusters and/or databases, won't that require a significant refactor of my existing code?
  • What's the most effective way to manage my indexes? Should they be created manually, or within my C# code? How can I ensure that indexes are synchronized with my code?
  • If I have multiple C# applications, how can I prevent MongoDB setup code duplication?
  • How can I instrument my code, considering the MongoDB C# driver doesn't natively support OpenTelemetry?
  • How can I execute integration tests in an isolated environment? Using a shared database requires cleanup, leading to unreliable test results.

Workleap.Extensions.Mongo was developed to address these challenges. We offer a straightforward, flexible, and standard approach to adding and configuring the MongoDB C# driver in your C# projects. Here is an overview of the features of Workleap.Extensions.Mongo:

  • Support for dependency injection: We use Microsoft's modern dependency injection (IServiceCollection) to expose MongoDB's classes and interfaces.
  • Standardized configuration: We leverage Microsoft's configuration, enabling easy configuration of MongoDB settings via diverse configuration providers of your choice. The options pattern simplifies overriding and extending any setting, including static MongoDB settings (custom serializers and convention packs).
  • Support for multiple MongoDB clusters and/or databases: You won't need to refactor your entire codebase to support multiple MongoDB data sources - it's supported by default.
  • Elimination of boilerplate and duplicated code: Remove redundant, copy-pasted MongoDB C# code from your codebase, enabling you to focus on actually utilizing the driver.
  • Built-in instrumentation: We provide built-in support for OpenTelemetry instrumentation, adhering to OpenTelemetry's semantic conventions for MongoDB. Additionally, we offer an extra NuGet package for Application Insights .NET SDK support.
  • Optional index management: Declare indexes in your C# code, and then use our C# API to automatically create and update indexes based on what's declared in your code. Built-in Roslyn analyzers will assist developers in considering new indexes.
  • Optional async enumerables support: You can simplify your code by using our extension methods that employ IAsyncEnumerable rather than more verbose MongoDB cursors.
  • Optional field-level encryption: Implement your own encrypt and decrypt methods, which can then automatically encrypt annotated MongoDB document fields for at-rest security.
  • Optional ephemeral database for integration tests: Each of your integration test methods can have its own new MongoDB database operating locally.

Getting started

We offer three main NuGet packages:

Firstly, Workleap.Extensions.Mongo, is the package that you'd ideally install in your startup project, where your main method resides. This is where you would incorporate our library into your dependency injection services and link your configuration to our option classes:

// Method 1: Directly set the options values with C# code
services.AddMongo(options =>
{
    options.ConnectionString = "[...]";
    options.DefaultDatabaseName = "marketing";
});

// Method 2: Bind the options values to a configuration section
services.AddMongo(configuration.GetRequiredSection("Mongo").Bind);

// Method 3: Lazily bind the options values to a configuration section
services.AddMongo();
services.AddOptions<MongoClientOptions>().Bind(configuration.GetRequiredSection("Mongo"));

// appsettings.json (or any other configuration source such as environment variables or Azure KeyVault)
{
  "Mongo": {
    "ConnectionString": "[...]",
    "DefaultDatabaseName": "marketing"
  }
}

// Method 4: Implement IConfigureNamedOptions<MongoClientOptions>:
// https://learn.microsoft.com/en-us/dotnet/core/extensions/options#use-di-services-to-configure-options

The second NuGet package, Workleap.Extensions.Mongo.Abstractions, only provides abstractions and extension methods, aligning with the principles of Microsoft's extension libraries such as Microsoft.Extensions.Logging.Abstractions and Microsoft.Extensions.Configuration.Abstractions. You would typically install this package in your domain-specific .NET projects to avoid unnecessary NuGet dependencies. This package already includes the MongoDB C# driver, so there's no need to install it separately (unless you need a specific version).

// 1) Directly inject a collection bound to the default database
var people = serviceProvider.GetRequiredService<IMongoCollection<PersonDocument>>();

// 2) You can inject the default database
var people = serviceProvider.GetRequiredService<IMongoDatabase>().GetCollection<PersonDocument>();

// 3) You can inject the default client
var people = serviceProvider.GetRequiredService<IMongoClient>()
    .GetDatabase("marketing").GetCollection<PersonDocument>();

// 4) Finally, you can inject a specific client for a specific registered MongoDB cluster
// More on that later in this document
var people = serviceProvider.GetRequiredService<IMongoClientProvider>()
    .GetClient("mycluster").GetDatabase("marketing").GetCollection<PersonDocument>();

[MongoCollection("people")]
public class PersonDocument : IMongoDocument // IMongoDocument is an empty marker interface (required)
{
    // [...]
}

The third NuGet package, Workleap.Extensions.Mongo.Ephemeral, is designed for your integration tests. It can be utilized whenever you require a real yet ephemeral MongoDB cluster with a single node replica set. Through dependency injection, each integration test method can have access to a unique and isolated database.

Adding and configuring MongoDB clients

When registering your dependency injection services, you can invoke services.AddMongo(...) as demonstrated in the previous section. This action registers MongoDB dependencies and the main MongoDB cluster by providing the connection string and default (primary) database name.

It is also possible to register multiple additional MongoDB clusters:

services.AddMongo(options => { /* [...] */ })
    .AddNamedClient("anotherCluster", options => { /* [...] */ })
    .AddNamedClient("andAnotherOne", options => { /* [...] */ });

// There are many ways to configure these named options
services.AddOptions<MongoClientOptions>("anotherCluster").Bind(configuration.GetRequiredSection("Mongo:AnotherCluster"));
services.AddOptions<MongoClientOptions>("andAnotherOne").Bind(configuration.GetRequiredSection("Mongo:AndAnotherOne"));

The MongoClientOptions option class further permits you to configure the MongoClientSettings for a cluster:

services.AddMongo(options =>
{
    options.MongoClientSettingsConfigurator = settings =>
    {
        settings.ApplicationName = "myapp";

        settings.ClusterConfigurator = cluster =>
        {
            // [...]
        };
    };
});

While MongoClientOptions is the option class for configuring a specific MongoDB cluster connection, MongoStaticOptions is available to customize MongoDB static options such as BSON serializers and convention packs for the entire application:

services.AddMongo().ConfigureStaticOptions(options =>
{
    // There are built-in serializers and conventions registered, but you can remove or override them
    // ⚠ Caution, these are objects that will live for the entire lifetime of the application (singleton) as the MongoDB C# driver
    // uses static properties to configure its behavior and serialization
    options.GuidRepresentationMode = GuidRepresentationMode.V2; // V3 is the default
    options.BsonSerializers[typeof(Guid)] = new GuidSerializer(GuidRepresentation.Standard);
    options.ConventionPacks.Add(new MyConventionPack());
});

You can also use different options patterns to configure static options:

services.AddOptions<MongoStaticOptions>().Configure(options => { /* [...] */ })

Declaring and using MongoDB documents and collections

The process doesn't deviate much from the standard way of declaring and using MongoDB collections in C#. However, there are two additional steps:

  • You must decorate the document class with the MongoCollectionAttribute to specify the collection name,
  • The document class must implement the empty marker interface IMongoDocument for generic constraints purposes.
[MongoCollection("people")]
public class PersonDocument : IMongoDocument
{
    // [...]
}

Refer back to the getting started section to learn how to resolve IMongoCollection<TDocument> from the dependency injection services.

We also provide IAsyncEnumerable<TDocument> extensions on IAsyncCursor<TDocument> and IAsyncCursorSource<TDocument>, eliminating the need to deal with cursors. For example:

var people = await collection.Find(FilterDefinition<PersonDocument>.Empty).ToAsyncEnumerable();

Logging and distributed tracing

Workleap.Extensions.Mongo supports modern logging with ILogger and log level filtering. MongoDB commands can be logged at the Debug level and optionally with their BSON content only if you set MongoClientOptions.Telemetry.CaptureCommandText to true.

Distributed tracing with OpenTelemetry is also integrated. We follow the semantic conventions for MongoDB. You can simply observe activities (traces) originating from our Workleap.Extensions.Mongo assembly.

We also support distributed tracing with the Application Insights .NET SDK. To enable this feature, you need to install the additional Workleap.Extensions.Mongo.ApplicationInsights NuGet package. Simply use the .AddApplicationInsights() on the builder object returned by services.AddMongo():

services.AddMongo().AddApplicationInsights();

By default, some commands such as isMaster, buildInfo, saslStart, etc., are ignored by our instrumentation. You can either ignore additional commands or undo the ignoring of commands by modifying the MongoClientOptions.Telemetry.DefaultIgnoredCommandNames collection.

Index management

We provide a mechanism for you to declare your collection indexes and ensure they are applied to your database. To do this, declare your indexes by implementing a custom MongoIndexProvider<TDocument>:

[MongoCollection("people", IndexProviderType = typeof(PersonDocumentIndexes))]
public class PersonDocument : IMongoDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public string Name { get; set; } = string.Empty;
}

public class PersonDocumentIndexes : MongoIndexProvider<PersonDocument>
{
    public override IEnumerable<CreateIndexModel<PersonDocument>> CreateIndexModels()
    {
        // Index name is mandatory
        yield return new CreateIndexModel<PersonDocument>(
            Builders<PersonDocument>.IndexKeys.Combine().Ascending(x => x.Name),
            new CreateIndexOptions { Name = "name" });
    }
}

At this stage, nothing will happen. To actually create or update the index, you need to inject our IMongoIndexer service and then call one of its UpdateIndexesAsync() method overloads, for example:

var indexer = this.Services.GetRequiredService<IMongoIndexer>();
await indexer.UpdateIndexesAsync(new[] { typeof(PersonDocument) });

It is up to you to decide when and where to run the process of creating and updating the indexes. You could do it at the start of your application, in a separate application that runs in a continuous delivery pipeline, etc.

Our indexation engine handles:

  1. Discovering the CreateIndexModel<TDocument> declared in your code using reflection.
  2. Computing an unique index name based on the model (we append a hash to the provided index name, for example, name_512cbbb935626e2b4b7c44972597c4a8).
  3. Discovering existing indexes in the MongoDB collection.
  4. Comparing the names and hashes of both sides to determine if:
    1. We need to create a missing index.
    2. We need to drop and recreate an updated index (the hashes don't match).
    3. We need to drop an index that is no longer declared in your code.
  5. Leaving any other index intact. We only manage the indexes that have a name ending with a generated hash.
  6. Handling distributed race conditions. If many instances of an application call indexer.UpdateIndexesAsync() at the same time, only one will actually succeed (we use a distributed lock).

We include a Roslyn analyzer, detailed in a section below, that encourages developers to adorn classes that consume MongoDB collections with attributes (IndexByAttribute or NoIndexNeededAttribute). The aim is to increase awareness about which indexes should be used (or created) when querying MongoDB collections.

Support for inheritance

The indexer mechanism support document inheritance and different indexer for a same collection. For example:


[BsonKnownTypes(typeof(DogPersonDocument), typeof(DogPersonDocument))]
[MongoCollection("people", IndexProviderType = typeof(PersonDocumentIndexes))]
public class PersonDocument : IMongoDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public string Name { get; set; } = string.Empty;
}

public class PersonDocumentIndexes : MongoIndexProvider<PersonDocument>
{
    public override IEnumerable<CreateIndexModel<PersonDocument>> CreateIndexModels()
    {
        yield return new CreateIndexModel<PersonDocument>(
            Builders<PersonDocument>.IndexKeys.Combine().Ascending(x => x.Name),
            new CreateIndexOptions { Name = "name" });
    }
}

// No special indexer for this class
[BsonDiscriminator("Dog")]
public class DogPersonDocument : PersonDocument
{
    public int DogCount { get; set; } = string.Empty;
}

// Need to redefine MongoCollectionAttribute to use a different indexer
[BsonDiscriminator("Cat")]
[MongoCollection("people", IndexProviderType = typeof(CatDocumentIndexes))]
public class CatPersonDocument : PersonDocument
{
    public int CatCount { get; set; }
}

public class CatDocumentIndexes : MongoIndexProvider<PersonDocument>
{
    public override IEnumerable<CreateIndexModel<PersonDocument>> CreateIndexModels()
    {
        yield return new CreateIndexModel<PersonDocument>(
            Builders<PersonDocument>.IndexKeys.Combine().Ascending(x => x.CatCount),
            new CreateIndexOptions { Name = "cat_count" });
    }
}

Field encryption

The Workleap.Extensions.Mongo library supports field-level encryption at rest, which means you can specify in your C# code which document fields should be encrypted in your MongoDB database. Any C# property can be encrypted, as long as you provide how data gets encrypted and decrypted. These properties then become binary data in your documents.

To enable field-level encryption, simply decorate the sensitive properties with the [SensitiveInformationAttribute]:

[MongoCollection("people")]
public class PersonDocument : IMongoDocument
{
    // [...]

    [SensitiveInformation(SensitivityScope.User)] // Other values are "Tenant" and "Application"
    public string Address { get; set; } = string.Empty;
}

Next, create a class that implements IMongoValueEncryptor:

public class MyMongoalueEncryptor : IMongoValueEncryptor
{
    public byte[] Encrypt(byte[] bytes, SensitivityScope sensitivityScope)
    {
        // return protected bytes using the method of your choice
    }

    public byte[] Decrypt(byte[] bytes, SensitivityScope sensitivityScope)
    {
        // return unprotected bytes using the method of your choice
    }
}

Finally, register this class in the dependency injection services:

// This ends up registered using the singleton service lifetime
services.AddMongo().AddEncryptor<MyMongoalueEncryptor>();

Keep in mind that encrypted values become binary data, which can make querying them more difficult. You'll need to take this into account when designing your database schema and queries.

Ephemeral MongoDB databases for integration tests

When creating integration tests, instead of using a shared MongoDB database for all your tests, you could assign a brand new ephemeral database for each individual test method. This approach reduces test flakiness, prevents the state of one test from impacting others and remove the need for manual or automatic cleanup.

This is what our NuGet package Workleap.Extensions.Mongo.Ephemeral does when you invoke its UseEphemeralRealServer() method:

services.AddMongo().UseEphemeralRealServer();

When this method is called, each time a database or collection is requested within the scope of an individual IServiceProvider:

  • A MongoDB server starts (we use EphemeralMongo),
  • A randomly named database is provided to your code.

When you dispose of the IServiceProvider, the related resources are destroyed. We leverage internal caching to avoid running multiple instances of MongoDB servers concurrently, opting instead to reuse a single instance. This method allows you to run multiple concurrent tests, each with their own MongoDB database. If your test runner crashes, the MongoDB process will be terminated, preventing orphaned processes from consuming unnecessary resources.

Included Roslyn analyzers

Rule ID Category Severity Description
GMNG01 Design Warning Add 'IndexBy' or 'NoIndexNeeded' attributes on the containing type

To modify the severity of one of these diagnostic rules, you can use a .editorconfig file. For example:

## Disable analyzer for test files
[**Tests*/**.cs]
dotnet_diagnostic.GMNG01.severity = none

To learn more about configuring or suppressing code analysis warnings, refer to this documentation.

License

Copyright © 2023, Workleap. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.11.2-preview.9 34 10/30/2024
1.11.2-preview.8 36 10/28/2024
1.11.2-preview.7 31 10/28/2024
1.11.2-preview.6 35 10/28/2024
1.11.2-preview.5 38 10/16/2024
1.11.2-preview.4 34 10/7/2024
1.11.2-preview.3 44 10/1/2024
1.11.2-preview.2 40 9/30/2024
1.11.2-preview.1 50 9/27/2024
1.11.1 1,567 9/20/2024
1.11.1-preview.2 36 9/20/2024
1.11.1-preview.1 36 9/20/2024
1.11.0 569 8/28/2024
1.10.1-preview.17 62 8/27/2024
1.10.1-preview.16 41 8/27/2024
1.10.1-preview.15 41 8/27/2024
1.10.1-preview.14 65 8/16/2024
1.10.1-preview.13 47 8/1/2024
1.10.1-preview.12 41 8/1/2024
1.10.1-preview.11 41 8/1/2024
1.10.1-preview.10 34 7/31/2024
1.10.1-preview.9 34 7/31/2024
1.10.1-preview.8 41 7/30/2024
1.10.1-preview.7 45 7/30/2024
1.10.1-preview.6 46 7/30/2024
1.10.1-preview.5 48 7/29/2024
1.10.1-preview.4 48 7/15/2024
1.10.1-preview.3 42 7/15/2024
1.10.1-preview.1 46 6/25/2024
1.10.0 1,210 6/20/2024
1.9.3-preview.6 52 6/20/2024
1.9.3-preview.5 48 6/17/2024
1.9.3-preview.4 52 6/10/2024
1.9.3-preview.3 51 5/31/2024
1.9.3-preview.2 49 5/31/2024
1.9.3-preview.1 57 5/29/2024
1.9.2 510 5/27/2024
1.9.2-preview.2 45 5/27/2024
1.9.1 122 5/16/2024
1.9.1-preview.2 60 5/16/2024
1.9.1-preview.1 57 5/10/2024
1.9.0 2,676 5/7/2024
1.8.1-preview.1 67 5/7/2024
1.8.0 281 5/6/2024
1.7.1-preview.1 68 5/6/2024
1.7.0 98 4/29/2024
1.6.1-preview.10 46 4/29/2024
1.6.1-preview.9 60 4/22/2024
1.6.1-preview.8 66 4/16/2024
1.6.1-preview.7 57 4/15/2024
1.6.1-preview.6 66 4/12/2024
1.6.1-preview.5 52 4/11/2024
1.6.1-preview.4 63 4/3/2024
1.6.1-preview.3 60 3/8/2024
1.6.1-preview.2 61 3/7/2024
1.6.1-preview.1 66 3/4/2024
1.6.0 251 2/29/2024
1.5.3-preview.8 62 2/29/2024
1.5.3-preview.7 52 2/19/2024
1.5.3-preview.5 56 2/16/2024
1.5.3-preview.4 69 2/12/2024
1.5.3-preview.3 67 2/7/2024
1.5.3-preview.2 68 2/6/2024
1.5.3-preview.1 61 2/5/2024
1.5.2 369 1/29/2024
1.5.2-preview.7 56 1/29/2024
1.5.2-preview.6 53 1/26/2024
1.5.2-preview.5 58 1/24/2024
1.5.2-preview.4 54 1/22/2024
1.5.2-preview.3 61 1/12/2024
1.5.2-preview.2 58 1/12/2024
1.5.2-preview.1 65 12/18/2023
1.5.1 918 12/5/2023
1.5.1-preview.4 79 12/4/2023
1.5.1-preview.3 74 11/26/2023
1.5.1-preview.2 70 11/21/2023
1.5.1-preview.1 65 11/17/2023
1.5.0 204 11/16/2023
1.4.1-preview.9 67 11/15/2023
1.4.1-preview.8 63 11/13/2023
1.4.1-preview.7 66 11/7/2023
1.4.1-preview.6 72 10/30/2023
1.4.1-preview.5 76 10/13/2023
1.4.1-preview.4 78 10/13/2023
1.4.1-preview.3 70 10/6/2023
1.4.1-preview.2 76 9/27/2023
1.4.1-preview.1 79 9/26/2023
1.4.0 359 9/25/2023
1.3.3-preview.6 74 9/25/2023
1.3.3-preview.5 75 9/25/2023
1.3.3-preview.4 78 9/21/2023
1.3.3-preview.3 71 9/20/2023
1.3.3-preview.2 75 9/20/2023
1.3.3-preview.1 81 9/11/2023
1.3.2 618 9/6/2023
1.3.2-preview.1 58 9/6/2023
1.3.1 165 7/20/2023
1.3.1-preview.4 91 7/20/2023
1.3.1-preview.3 72 7/20/2023