From 8cba3d8300d2279ed4072992457d742621a3137e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 21 Jan 2021 18:00:01 +0300 Subject: [PATCH] Update MongoDB document fow usage of async queryable. --- docs/en/Entity-Framework-Core.md | 3 +- docs/en/MongoDB.md | 56 +++++++++++++++++++------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/docs/en/Entity-Framework-Core.md b/docs/en/Entity-Framework-Core.md index d1cc5e1a6f..a2c0e79996 100644 --- a/docs/en/Entity-Framework-Core.md +++ b/docs/en/Entity-Framework-Core.md @@ -236,7 +236,8 @@ public class BookRepository public async Task DeleteBooksByType(BookType type) { - await DbContext.Database.ExecuteSqlRawAsync( + var dbContext = await GetDbContextAsync(); + await dbContext.Database.ExecuteSqlRawAsync( $"DELETE FROM Books WHERE Type = {(int)type}" ); } diff --git a/docs/en/MongoDB.md b/docs/en/MongoDB.md index 7fae9c3892..60b6a94823 100644 --- a/docs/en/MongoDB.md +++ b/docs/en/MongoDB.md @@ -149,7 +149,7 @@ public class Book : AggregateRoot } ``` -(`BookType` is a simple enum here) And you want to create a new `Book` entity in a [domain service](Domain-Services.md): +(`BookType` is a simple `enum` here) And you want to create a new `Book` entity in a [domain service](Domain-Services.md): ```csharp public class BookManager : DomainService @@ -215,7 +215,8 @@ public class BookRepository : BookType type, CancellationToken cancellationToken = default(CancellationToken)) { - await Collection.DeleteManyAsync( + var collection = await GetCollectionAsync(cancellationToken); + await collection.DeleteManyAsync( Builders.Filter.Eq(b => b.Type, type), cancellationToken ); @@ -253,7 +254,7 @@ public async override Task DeleteAsync( ### Access to the MongoDB API -In most cases, you want to hide MongoDB APIs behind a repository (this is the main purpose of the repository). However, if you want to access the MongoDB API over the repository, you can use `GetDatabase()` or `GetCollection()` extension methods. Example: +In most cases, you want to hide MongoDB APIs behind a repository (this is the main purpose of the repository). However, if you want to access the MongoDB API over the repository, you can use `GetDatabaseAsync()` or `GetCollectionAsync()` extension methods. Example: ```csharp public class BookService @@ -265,10 +266,10 @@ public class BookService _bookRepository = bookRepository; } - public void Foo() + public async Task FooAsync() { - IMongoDatabase database = _bookRepository.GetDatabase(); - IMongoCollection books = _bookRepository.GetCollection(); + IMongoDatabase database = await _bookRepository.GetDatabaseAsync(); + IMongoCollection books = await _bookRepository.GetCollectionAsync(); } } ``` @@ -390,36 +391,45 @@ If you have better logic or using an external library for bulk operations, you c - You may use example template below: ```csharp -public class MyCustomMongoDbBulkOperationProvider : IMongoDbBulkOperationProvider, ITransientDependency +public class MyCustomMongoDbBulkOperationProvider + : IMongoDbBulkOperationProvider, ITransientDependency { - public async Task DeleteManyAsync(IMongoDbRepository repository, - IEnumerable entities, - IClientSessionHandle sessionHandle, - bool autoSave, - CancellationToken cancellationToken) + public async Task DeleteManyAsync( + IMongoDbRepository repository, + IEnumerable entities, + IClientSessionHandle sessionHandle, + bool autoSave, + CancellationToken cancellationToken) where TEntity : class, IEntity { // Your logic here. } - public async Task InsertManyAsync(IMongoDbRepository repository, - IEnumerable entities, - IClientSessionHandle sessionHandle, - bool autoSave, - CancellationToken cancellationToken) + public async Task InsertManyAsync( + IMongoDbRepository repository, + IEnumerable entities, + IClientSessionHandle sessionHandle, + bool autoSave, + CancellationToken cancellationToken) where TEntity : class, IEntity { // Your logic here. } - public async Task UpdateManyAsync(IMongoDbRepository repository, - IEnumerable entities, - IClientSessionHandle sessionHandle, - bool autoSave, - CancellationToken cancellationToken) + public async Task UpdateManyAsync( + IMongoDbRepository repository, + IEnumerable entities, + IClientSessionHandle sessionHandle, + bool autoSave, + CancellationToken cancellationToken) where TEntity : class, IEntity { // Your logic here. } } -``` \ No newline at end of file +``` + +## See Also + +* [Entities](Entities.md) +* [Repositories](Repositories.md) \ No newline at end of file