QDev.CSharp.MongoDB.RepositoryPattern
1.0.1
dotnet add package QDev.CSharp.MongoDB.RepositoryPattern --version 1.0.1
NuGet\Install-Package QDev.CSharp.MongoDB.RepositoryPattern -Version 1.0.1
<PackageReference Include="QDev.CSharp.MongoDB.RepositoryPattern" Version="1.0.1" />
paket add QDev.CSharp.MongoDB.RepositoryPattern --version 1.0.1
#r "nuget: QDev.CSharp.MongoDB.RepositoryPattern, 1.0.1"
// Install QDev.CSharp.MongoDB.RepositoryPattern as a Cake Addin #addin nuget:?package=QDev.CSharp.MongoDB.RepositoryPattern&version=1.0.1 // Install QDev.CSharp.MongoDB.RepositoryPattern as a Cake Tool #tool nuget:?package=QDev.CSharp.MongoDB.RepositoryPattern&version=1.0.1
Database Manager MongoDB
A library for implementing generic repositories and unit of work with MongoDB.Driver
.
This implementation uses a singleton setting instance of the IMongoDbSetting
.
Table of Contents
- Installation
- Injecting the RepositoryPattern's Services
- Creating the Service
- Unit of Work
- Repository
Installation
Using the NuGet package manager console within Visual Studio run the following command:
Install-Package QDev.CSharp.MongoDB.RepositoryPattern
Or using the .NET Core CLI from a terminal window:
dotnet add package QDev.CSharp.MongoDB.RepositoryPattern
Injecting the RepositoryPattern's Services
Creating the Settings
Initially, it's essential to create a class that implements the IMongoSettings interface. Following that, configure a dedicated section within the appsettings.json file to store the necessary settings for configuring the MongoDB client in your development project.
public class MyMongoSettings : IMongoSettings
{
public string Server { get; set; }
public string Database { get; set; }
}
{
"MongoDB": {
"Server": "localhost:27017",
"Database": "test"
}
}
Injecting the Services
Here is the code snippet to inject the required services for the seamless operation of the repository pattern in your development project:
builder.Services.AddRepositoryPattern<MyMongoSettings>(builder.Configuration, "MongoDB"); // Keep in mind that if the 'sectionName' parameter is null,
// it will default to the name of the class that implements 'IMongoSettings' in your development context
Creating the Service
To create a service class it's mandatory use this form Service<TDocument>
as shown in the following example.
Example:
public class MyService<MyDocument>
{
private readonly IUnitOfWork _unitOfWork;
public MyService(IServiceProvider services)
{
var scoped = provider.CreateScope();
_unitOfWork = scoped.ServiceProvider.GetRequiredService<IUnitOfWork>();
}
public async Task<MyDocument> CreateMyDocument(MyDocument document, CancellationToken cancellationToke = default)
{
var newDocument await _unitOfWork.Repository<TDocument>().InsertOneAsync(document, cancellationToken);
await _unitOfWork.SaveAsync(cancellationToken);
return newDocument;
}
// and more methods as needed
}
Unit Of Work
IUnitOfWork
represents a unit of work for coordinating and managing database operations using a custom repository pattern.
Please note that this interface provides methods and a property to manage transactions and repositories in a custom repository pattern. It allows for both synchronous and asynchronous transaction management.
Functions
Function | Description |
---|---|
IRepository<TDocument> Repository<TDocument>() |
Retrieves a repository for managing documents of the specified type. |
void BeginTransaction() |
Initiates a new database transaction. |
void AbortTransaction() |
Aborts and rolls back the current transaction. |
Task AbortTransactionAsync() |
Asynchronously aborts and rolls back the current transaction. |
void CommitTransaction() |
Commits the current transaction. |
Task CommitTransactionAsync() |
Asynchronously commits the current transaction. |
Properties
Property | Description |
---|---|
bool IsInTransaction |
Gets a boolean value indicating whether a transaction is currently in progress. |
Repository
IRepository<TDocument>
represents a repository for managing documents of typeTDocument
within a MongoDB context using the MongoDB C# driver.TDocument
: The type of documents managed by this repository.
Methods
Method | Description |
---|---|
Task<IAsyncCursor<TDocument?>> GetManyAsync(FilterDefinition<TDocument>? filter = null, FindOptions<TDocument, TDocument>? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously retrieves multiple documents from the repository based on filter criteria. |
Task<TDocument?> GetOneAsync(FilterDefinition<TDocument>? filter = null, FindOptions<TDocument, TDocument>? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously retrieves a single document from the repository based on filter criteria. |
Task<TDocument> InsertOneAsync(TDocument document, InsertOneOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously inserts a single document into the repository. |
Task InsertManyAsync(IEnumerable<TDocument> documents, InsertManyOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously inserts multiple documents into the repository. |
Task UpdateOneAsync(UpdateDefinition<TDocument> update, FilterDefinition<TDocument>? filter = null, UpdateOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously updates a single document in the repository. |
Task UpdateManyAsync(UpdateDefinition<TDocument> update, FilterDefinition<TDocument>? filter = null, UpdateOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously updates multiple documents in the repository. |
Task<TDocument> ReplaceOneAsync(TDocument document, FilterDefinition<TDocument>? filter = null, ReplaceOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously replaces a single document in the repository. |
Task RemoveOneAsync(FilterDefinition<TDocument>? filter = null, DeleteOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously removes a single document from the repository based on filter criteria. |
Task RemoveManyAsync(FilterDefinition<TDocument>? filter = null, DeleteOptions? options = null, IClientSessionHandle? session = null, CancellationToken cancellationToken = default) |
Asynchronously removes multiple documents from the repository based on filter criteria. |
Please note that some functions include optional parameters to configure various options, and there's a description of exception handling for error scenarios.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.0
- Humanizer (>= 2.14.1)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0.0)
- MongoDB.Driver (>= 2.22.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
fix documentation