From c14ceffe4fe9575005684439eceb43428c3aca41 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen An Date: Tue, 26 Oct 2021 18:14:42 +0700 Subject: [PATCH] Add read operation support for filter definition --- .../Abstractions/IBaseReadOnlyRepository.cs | 96 +++++++++++++ .../IReadOnlyMongoRepository.TKey.cs | 1 + .../DataAccess/Read/MongoDbReader.Main.cs | 134 ++++++++++++++++++ .../BaseMongoRepository.TKey.ReadOnly.cs | 1 + .../ReadOnlyMongoRepository.cs | 115 +++++++++++++++ 5 files changed, 347 insertions(+) diff --git a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs index 304607f..185ef77 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs @@ -49,6 +49,30 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + where TDocument : IDocument where TKey : IEquatable; + /// /// Asynchronously returns one document given an expression filter. /// @@ -83,6 +107,29 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// An optional cancellation Token. + Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + bool Any(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null) + where TDocument : IDocument where TKey : IEquatable; /// /// Returns true if any of the document of the collection matches the filter condition. @@ -107,6 +154,30 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetAllAsync(FilterDefinition condition, + FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + List GetAll(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + where TDocument : IDocument where TKey : IEquatable; + /// /// Asynchronously returns a list of the documents matching the filter condition. /// @@ -130,6 +201,31 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// An optional cancellation Token. + Task CountAsync(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + long Count(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null) + where TDocument : IDocument where TKey : IEquatable; + /// /// Asynchronously counts how many documents match the filter condition. /// diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index 9b311f0..a61374c 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index 832b4ae..c343668 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -57,6 +57,39 @@ namespace MongoDbGenericRepository.DataAccess.Read return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(); } + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + public virtual Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefaultAsync(cancellationToken); + } + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + public virtual TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefault(); + } + /// /// Asynchronously returns one document given an expression filter. /// @@ -100,6 +133,41 @@ namespace MongoDbGenericRepository.DataAccess.Read return HandlePartitioned(partitionKey).Find(filter); } + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// An optional cancellation Token. + public virtual async Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var count = await HandlePartitioned(partitionKey).CountDocumentsAsync(condition, countOption, cancellationToken); + return count > 0; + } + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + public virtual bool Any(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var count = HandlePartitioned(partitionKey).CountDocuments(condition, countOption); + return count > 0; + } + /// /// Returns true if any of the document of the collection matches the filter condition. /// @@ -131,6 +199,39 @@ namespace MongoDbGenericRepository.DataAccess.Read return (count > 0); } + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + public virtual Task> GetAllAsync(FilterDefinition condition, + FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).Find(condition, findOption).ToListAsync(cancellationToken); + } + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + public virtual List GetAll(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).Find(condition, findOption).ToList(); + } + /// /// Asynchronously returns a list of the documents matching the filter condition. /// @@ -160,6 +261,39 @@ namespace MongoDbGenericRepository.DataAccess.Read return HandlePartitioned(partitionKey).Find(filter).ToList(); } + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// An optional cancellation Token. + public virtual Task CountAsync(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).CountDocumentsAsync(condition, countOption, cancellationToken); + } + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + public virtual long Count(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).CountDocuments(condition, countOption); + } + /// /// Asynchronously counts how many documents match the filter condition. /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index e4e373e..cdbb862 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index 9c9e33e..1959003 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -71,6 +71,35 @@ namespace MongoDbGenericRepository { return MongoDbReader.GetById(id, partitionKey); } + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + public Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetOneAsync(condition, findOption, partitionKey, cancellationToken); + } + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + public TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetOne(condition, findOption, partitionKey); + } /// /// Asynchronously returns one document given an expression filter. @@ -114,6 +143,34 @@ namespace MongoDbGenericRepository { return MongoDbReader.GetCursor(filter, partitionKey); } + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// An optional cancellation Token. + public Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.AnyAsync(condition, countOption, partitionKey, cancellationToken); + } + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + public bool Any(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.Any(condition, countOption, partitionKey); + } /// /// Returns true if any of the document of the collection matches the filter condition. @@ -143,6 +200,35 @@ namespace MongoDbGenericRepository { return MongoDbReader.Any(filter, partitionKey); } + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + public Task> GetAllAsync(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetAllAsync(condition, findOption, partitionKey, cancellationToken); + } + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + public List GetAll(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetAll(condition, findOption, partitionKey); + } /// /// Asynchronously returns a list of the documents matching the filter condition. @@ -172,6 +258,35 @@ namespace MongoDbGenericRepository { return MongoDbReader.GetAll(filter, partitionKey); } + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// An optional cancellation Token. + public Task CountAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.CountAsync(condition, countOption, partitionKey, cancellationToken); + } + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + public long Count(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.Count(condition, countOption, partitionKey); + } /// /// Asynchronously counts how many documents match the filter condition.