MongoDB.Driver.Extensions
1.0.1
See the version list below for details.
dotnet add package MongoDB.Driver.Extensions --version 1.0.1
NuGet\Install-Package MongoDB.Driver.Extensions -Version 1.0.1
<PackageReference Include="MongoDB.Driver.Extensions" Version="1.0.1" />
paket add MongoDB.Driver.Extensions --version 1.0.1
#r "nuget: MongoDB.Driver.Extensions, 1.0.1"
// Install MongoDB.Driver.Extensions as a Cake Addin #addin nuget:?package=MongoDB.Driver.Extensions&version=1.0.1 // Install MongoDB.Driver.Extensions as a Cake Tool #tool nuget:?package=MongoDB.Driver.Extensions&version=1.0.1
MongoDB.Driver.Extensions
MongoDB.Driver.Extensions is a library that extends MongoDB.Driver allowing you a set of functionality needed by common applications. The library is completely compatible with the .Net Standard 2.0
What can it be used for?
The idea behind this library is to make easier the common operation around a document you have persisted into MongoDb.
For example you have:
- Save or update a document;
- Insert a new document;
- Update a document;
- Delete a document;
- Check if a document exists into your database;
- Insert many documents in a single roundtrip;
- Update many documents in a single roundtrip;
- Retrieve a document by Id;
- Handling pagination;
- Count the number of documents;
All the methods available to do in the list above are available in both sync / async version and offers different parameters in order to change the amount of data to work.
##How to install it MongoDB.Driver.Extensions is available via NuGet, so to install is enough to do
PM> Install-Package MongoDB.Driver.Extensions
How to configure it
To use this library the first is to provide all the necessary information to the library. To do that the first thing to do is to create your document:
public class User : DocumentBase<ObjectId>
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public Guid CompanyId { get; set; }
}
In this example I'm using an
ObjectId
and database key, but of course you can change it with your favourite type (string, Guid, and so on).
Now is time to create your repository:
internal class UserRepository : RepositoryBase<User,ObjectId>
{
public UserRepository(MongoDbDatabaseConfiguration configuration,
IMongoClient mongoClient)
: base(configuration,
mongoClient,
"MyDatabaseName")
{
}
}
The next step is the configuration and the IMongoClient
:
var conf = new MongoDbDatabaseConfiguration();
conf.ConnectionString = "mongodb://localhost:27017
IMongoClient client = new MongoClient(conf.ConnectionString);
If you are using Castle Windsort is enough to do something like this:
container.Register(Component.For<MongoDbDatabaseConfiguration>()
.Instance(conf)
.LifestyleSingleton());
container.Register(Component.For<IMongoClient>()
.Instance(client)
.LifestyleSingleton());
container.Register(Component.For<IRepository<User,ObjectId>>()
.ImplementedBy<UserRepository>()
.LifestyleSingleton());
In you are in Asp.Net Core:
services.AddSingleton<MongoDbDatabaseConfiguration>(conf);
services.AddSingleton<IMongoClient>(client);
services.AddSingleton<IRepository<User,ObjectId>, UserRepository>(client);
Now, in your service, you can do someting like this:
public class MyService : IMyService
{
private readonly IRepository<User,ObjectId> userRepository;
public MyService(IRepository<User,ObjectId> userRepository)
{
this.userRepository = userRepository;
}
public Task<IPagedResult<User>> DoSomething(int pageIndex, int pageSize, Guid companyId)
{
var request = new SimplePagedRequest();
request.PageIndex = pageIndex;
request.PageSize = pageSize;
var filter = Builders<User>.Filter.Eq(x => x.CompanyId, companyId);
return this.userRepository.GetPagedListAsync(request,filter);
}
}
DB name (Conventions and overrides)
Sometimes you have the same MongoDb Instance for all environments (I know this is not good) but you have to be sure you are using a different db name in order to work on new feature or test something that is almost ready for production.
For example for the User db you could have something like:
- Users (production environment);
- UsersTest (test environment);
- UserDev (dev environment);
As you saw in the example of UserRepository
you can specify the name of the database you want to use; it means all you need to do is change this with UserDev
and it works with Dev.
The bad part of this is that you have to change the name everytime you need to switch between the different environments.
Of course this is possible using this library without hardcode the db name into your code or adding tons of if.
What you have to do is to add the suffix you want into the MongoDbDatabaseConfiguration
class;
var conf = new MongoDbDatabaseConfiguration();
conf.ConnectionString = "mongodb://localhost:27017
conf.EnvironmentSuffix = "Dev";
In this case, when you specify the database on your repository, is enough to say Users
the repo will translate it in UsersDev
;
If you want to override this behaviour with something more complicated of course you can just creating a class that inherits from
IMongoDbNamingHelper
apply your logic and register it to you dependency injection container
##Collection Name (Conventions and overrides)
Out of the box the collection name is calculated using the name of the document pluralised. In the example of the User
the collection is Users
, for a document called vehicle
the collection name is vehicles
and so on.
If you want to change this behaviour you have two possible ways:
- Force the collection name in your repository;
- Add a dynamic behaviour;
Force collection name
internal class UserRepository : RepositoryBase<User,string>, IRepository<User,string>
{
public UserRepository(MongoDbDatabaseConfiguration configuration,
IMongoClient mongoClient)
: base(configuration,
mongoClient,
"MyDatabase",
null,
"MyCollectionName")
{
}
}
Claculate dinamically the name of the collection
Like mentioned for the database name, the same is valid here, just create a class that inherits from IMongoDbNamingHelper
apply your logic and register it to you dependency injection container
IMongoDbNamingHelper
As you understood the IMongoDbNamingHelper
has an important role in this library, here the code of the default implementation:
internal class DefaultMongoDbNamingHelper : IMongoDbNamingHelper
{
public string GetDatabaseName(MongoDbDatabaseConfiguration configuration, string dbName)
{
return string.Concat(dbName,configuration.EnvironmentSuffix);
}
public string GetCollectionName(string requiredCollection)
{
return requiredCollection?.Pluralize().ToLowerInvariant();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. |
-
.NETStandard 2.0
- Humanizer.Core (>= 2.5.16)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.2.0)
- MongoDB.Driver (>= 2.7.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MongoDB.Driver.Extensions:
Package | Downloads |
---|---|
Elsa.MongoDb
Provides MongoDB implementations of various abstractions from various modules. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on MongoDB.Driver.Extensions:
Repository | Stars |
---|---|
elsa-workflows/elsa-core
A .NET workflows library
|