Workleap.Extensions.Mongo.Ephemeral
1.3.1-preview.3
Prefix Reserved
See the version list below for details.
dotnet add package Workleap.Extensions.Mongo.Ephemeral --version 1.3.1-preview.3
NuGet\Install-Package Workleap.Extensions.Mongo.Ephemeral -Version 1.3.1-preview.3
<PackageReference Include="Workleap.Extensions.Mongo.Ephemeral" Version="1.3.1-preview.3" />
paket add Workleap.Extensions.Mongo.Ephemeral --version 1.3.1-preview.3
#r "nuget: Workleap.Extensions.Mongo.Ephemeral, 1.3.1-preview.3"
// Install Workleap.Extensions.Mongo.Ephemeral as a Cake Addin #addin nuget:?package=Workleap.Extensions.Mongo.Ephemeral&version=1.3.1-preview.3&prerelease // Install Workleap.Extensions.Mongo.Ephemeral as a Cake Tool #tool nuget:?package=Workleap.Extensions.Mongo.Ephemeral&version=1.3.1-preview.3&prerelease
Workleap.Extensions.Mongo
Provides MongoDB access through .NET dependency injection, following Microsoft.Extensions.*
library practices with several features:
- Automatic indexes creation, update and removal based on code changes
- Encryption at field level with different scopes (per user, tenant, or application-wide)
- Dependency-injection enabled using
IServiceCollection
andIServiceProvider
- Highly configurable (custom serializers, conventions, multiple databases support)
- Support for multiple MongoDB connection strings and MongoDB clients
- Roslyn analyzers for increased awareness of index usage
IAsyncEnumerable
support
Getting started
Install the package Workleap.Extensions.Mongo.Abstractions
in the project where you'll declare your documents.
This package contains base classes and interfaces such as IMongoDocument
, MongoIndexProvider
, MongoCollectionAttribute
.
There's also a few extension methods of the MongoDB C# driver classes and interfaces that adds IAsyncEnumerable
support to cursors.
Install the package Workleap.Extensions.Mongo
at the application entry point level to register and configure the dependencies in a IServiceCollection
.
Install the package Workleap.Extensions.Mongo.Ephemeral
whenever you want to use a real but ephemeral MongoDB cluster with a single node replica set.
This is ideal for integration testing, as each IServiceProvider
will have access to an unique and isolated database.
Example
// In the project that contains the documents:
// 1) Declare the collection name (camelCase) and the type responsible for providing indexes (optional)
[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" });
}
}
// 2) In the project that configures the application:
var services = new ServiceCollection();
services
.AddMongo(ConfigureDefaultMongoClient) // <-- register the default MongoDB client and database
.AddNamedClient("anotherClient", ConfigureAnotherMongoClient) // <-- (optional) register multiple MongoDB clients with different options and connection strings
.AddEncryptor<YourMongoValueEncryptor>() // (optional) <-- specify how to encrypt sensitive fields
.ConfigureStaticOptions(ConfigureMongoStatic); // (optional) <-- specify MongoDB C# driver static settings
private static void ConfigureDefaultMongoClient(MongoClientOptions options)
{
// Simplified for demo purposes, it is better to use appsettings.json, secret vaults
// and IConfigureOptions<> classes that can use dependency injection to access other options or dependencies
options.ConnectionString = "mongodb://localhost";
options.DefaultDatabaseName = "default";
options.EnableSensitiveInformationLogging = true;
// Used by the automatic index update feature
options.Indexing.LockMaxLifetimeInSeconds = 300;
options.Indexing.LockAcquisitionTimeoutInSeconds = 60;
// Modify MongoClientSettings (optional)
options.MongoClientSettingsConfigurator = settings => { };
// EXPERIMENTAL, FOR LOCAL DEVELOPMENT ONLY:
// This will output a warning log when a collection scan is detected on a "find" command
options.CommandPerformanceAnalysis.EnableCollectionScanDetection = true;
}
private static void ConfigureAnotherMongoClient(MongoClientOptions options)
{
// Define options relative to this non-default MongoDB client
// Ideally, use IConfigureNamedOptions<MongoClientOptions>
}
private static void ConfigureMongoStatic(MongoStaticOptions options)
{
// There are built-in serializers and conventions registered, but you can remove or override them
// ⚠ Careful, these are objects that will live for the entire lifetime of the application (singleton) as MongoDB C# driver
// uses static properties to configure its behavior and serialization
options.BsonSerializers[typeof(Foo)] = new MyFooBsonSerializer();
options.ConventionPacks.Add(new MyConventionPack());
}
// MongoDB document properties can be encrypted when decorated with the [SensitiveInformation(scope)] attribute
// There is a convention pack that use this class to encrypt and decrypt values using a custom BsonSerializer.
// This is not required if you never use the attribute.
private sealed class YourMongoValueEncryptor : IMongoValueEncryptor
{
// Encrypt and decrypt the bytes based on the sensitivity scope
// Use AsyncLocal<> to determine if the sensitivity scopes matches the current execution context.
// For instance, SensitivityScope.User should only work if there is actually an authenticated user detected through IHttpContextAccessor,
// or any other ambient mechanism that relies on AsyncLocal<>.
public byte[] Encrypt(byte[] bytes, SensitivityScope sensitivityScope) => bytes;
public byte[] Decrypt(byte[] bytes, SensitivityScope sensitivityScope) => bytes;
}
// 3) Consume the registered services
// Automatically update indexes if their definition in the code has changed - a cryptographic hash is used to detect changes.
// There's a distributed lock that prevents race conditions.
// UpdateIndexesAsync() also accepts an optional registered MongoDB client name, database name and/or cancellation token.
var indexer = this.Services.GetRequiredService<IMongoIndexer>();
await indexer.UpdateIndexesAsync(new[] { typeof(PersonDocument) });
await indexer.UpdateIndexesAsync(new[] { typeof(PersonDocument).Assembly }); // Assembly scanning alternative
// No need to know the collection name, just use the document type which must be decorated with MongoCollectionAttribute
var collection = this.Services.GetRequiredService<IMongoCollection<PersonDocument>>();
// OR: var collection = this.Services.GetRequiredService<IMongoDatabase>().GetCollection<PersonDocument>();
// No cursors handling needed, use IAsyncEnumerable
var people = await collection.Find(FilterDefinition<PersonDocument>.Empty).ToAsyncEnumerable();
// Access other registered MongoDB clients
var anotherMongoClient = this.Services.GetRequiredService<IMongoClientProvider>().GetClient("anotherClient");
// 4) Add the Workleap.Extensions.Mongo.Ephemeral package to use a ephemeral but real MongoDB database in your tests
var services = new ServiceCollection();
services.AddMongo().UseEphemeralRealServer();
Included Roslyn analyzers
Rule ID | Category | Severity | Description |
---|---|---|---|
GMNG01 | Design | Warning | Add 'IndexBy' or 'NoIndexNeeded' attributes on the containing type |
In order to change the severity of one of these diagnostic rules, use a .editorconfig
file, for instance:
## Disable analyzer for test files
[**Tests*/**.cs]
dotnet_diagnostic.GMNG01.severity = none
To learn more about how to configure or suppress code analysis warnings, read 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 | Versions 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. |
-
.NETFramework 4.7.2
- EphemeralMongo6 (>= 1.1.0)
- System.Memory (>= 4.5.5)
- Workleap.Extensions.Mongo (>= 1.3.1-preview.3)
-
.NETStandard 2.0
- EphemeralMongo6 (>= 1.1.0)
- System.Memory (>= 4.5.5)
- Workleap.Extensions.Mongo (>= 1.3.1-preview.3)
-
net6.0
- EphemeralMongo6 (>= 1.1.0)
- Workleap.Extensions.Mongo (>= 1.3.1-preview.3)
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 |