some good progress on refactoring into the KeyTyped repo for read only functionality
This commit is contained in:
@@ -0,0 +1,203 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository
|
||||||
|
{
|
||||||
|
public interface IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The connection string.
|
||||||
|
/// </summary>
|
||||||
|
string ConnectionString { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The database name.
|
||||||
|
/// </summary>
|
||||||
|
string DatabaseName { get; set; }
|
||||||
|
|
||||||
|
#region Read
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetByIdAsync<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given its id.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="id">The Id of the document you want to get.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns one document given an expression filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection cursor.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if any of the document of the collection matches the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a list of the documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Counts how many documents match the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Min / Max
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
|
TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,108 +10,8 @@ namespace MongoDbGenericRepository
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
|
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IReadOnlyMongoRepository
|
public interface IReadOnlyMongoRepository : IKeyTypedReadOnlyMongoRepository<Guid>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The connection string.
|
|
||||||
/// </summary>
|
|
||||||
string ConnectionString { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// The database name.
|
|
||||||
/// </summary>
|
|
||||||
string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
#region Read
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection cursor.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Read TKey
|
#region Read TKey
|
||||||
|
|
||||||
@@ -241,47 +141,6 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#region Min / Max
|
#region Min / Max
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByDescending">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -330,17 +189,6 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -353,17 +201,6 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -376,17 +213,6 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -400,17 +226,6 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>;
|
where TKey : IEquatable<TKey>;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -435,7 +250,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="selector">The field you want to sum.</param>
|
/// <param name="selector">The field you want to sum.</param>
|
||||||
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||||
Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
|
||||||
Expression<Func<TDocument, decimal>> selector,
|
Expression<Func<TDocument, decimal>> selector,
|
||||||
string partitionKey = null)
|
string partitionKey = null)
|
||||||
where TDocument : IDocument;
|
where TDocument : IDocument;
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
|
||||||
/// Its constructor must be given a connection string and a database name.
|
/// Its constructor must be given a connection string and a database name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class KeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
public abstract class KeyTypedReadOnlyMongoRepository<TKey> : IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The connection string.
|
/// The connection string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -41,6 +42,24 @@ namespace MongoDbGenericRepository
|
|||||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
|
protected KeyTypedReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||||
|
{
|
||||||
|
MongoDbContext = mongoDbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
|
protected KeyTypedReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
||||||
|
{
|
||||||
|
MongoDbContext = new MongoDbContext(mongoDatabase);
|
||||||
|
}
|
||||||
|
|
||||||
#region Read
|
#region Read
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -178,7 +197,10 @@ namespace MongoDbGenericRepository
|
|||||||
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return await GetByMaxAsync<TDocument>(filter, maxValueSelector, partitionKey);
|
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(maxValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -192,7 +214,10 @@ namespace MongoDbGenericRepository
|
|||||||
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return GetByMax<TDocument>(filter, maxValueSelector, partitionKey);
|
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(maxValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -205,7 +230,10 @@ namespace MongoDbGenericRepository
|
|||||||
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return await GetByMinAsync<TDocument>(filter, minValueSelector, partitionKey);
|
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(minValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -213,12 +241,15 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||||
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return GetByMin<TDocument>(filter, orderByAscending, partitionKey);
|
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(minValueSelector)
|
||||||
|
.Limit(1)
|
||||||
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -232,7 +263,7 @@ namespace MongoDbGenericRepository
|
|||||||
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return await GetMaxValueAsync<TDocument, TValue>(filter, maxValueSelector, partitionKey);
|
return await GetMaxMongoQuery<TDocument, TValue>(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -246,7 +277,21 @@ namespace MongoDbGenericRepository
|
|||||||
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return GetMaxValue<TDocument, TValue>(filter, maxValueSelector, partitionKey);
|
return GetMaxMongoQuery<TDocument, TValue>(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public virtual async Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return await GetMinMongoQuery<TDocument, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -260,7 +305,7 @@ namespace MongoDbGenericRepository
|
|||||||
public virtual TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
public virtual TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return GetMinValue<TDocument, TValue>(filter, minValueSelector, partitionKey);
|
return GetMinMongoQuery<TDocument, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -326,6 +371,40 @@ namespace MongoDbGenericRepository
|
|||||||
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
private IFindFluent<TDocument, TDocument> GetMinMongoQuery<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortBy(ConvertExpression(minValueSelector))
|
||||||
|
.Limit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||||
|
/// <param name="filter">A LINQ expression filter.</param>
|
||||||
|
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
private IFindFluent<TDocument, TDocument> GetMaxMongoQuery<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||||
|
where TDocument : IDocument<TKey>
|
||||||
|
{
|
||||||
|
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||||
|
.SortByDescending(ConvertExpression(maxValueSelector))
|
||||||
|
.Limit(1);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,8 +63,8 @@ namespace MongoDbGenericRepository
|
|||||||
Client = new MongoClient(connectionString);
|
Client = new MongoClient(connectionString);
|
||||||
Database = Client.GetDatabase(databaseName);
|
Database = Client.GetDatabase(databaseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
|
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="client">The MongoClient.</param>
|
/// <param name="client">The MongoClient.</param>
|
||||||
@@ -111,7 +111,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// Given the document type and the partition key, returns the name of the collection it belongs to.
|
/// Given the document type and the partition key, returns the name of the collection it belongs to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
/// <returns>The name of the collection.</returns>
|
/// <returns>The name of the collection.</returns>
|
||||||
private string GetCollectionName<TDocument>(string partitionKey)
|
private string GetCollectionName<TDocument>(string partitionKey)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,47 +15,29 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract partial class ReadOnlyMongoRepository
|
public abstract partial class ReadOnlyMongoRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The connection string.
|
|
||||||
/// </summary>
|
|
||||||
public string ConnectionString { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The database name.
|
|
||||||
/// </summary>
|
|
||||||
public string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The MongoDbContext
|
|
||||||
/// </summary>
|
|
||||||
protected IMongoDbContext MongoDbContext = null;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The constructor taking a connection string and a database name.
|
/// The constructor taking a connection string and a database name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||||
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||||
protected ReadOnlyMongoRepository(string connectionString, string databaseName)
|
protected ReadOnlyMongoRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
{
|
{
|
||||||
MongoDbContext = mongoDbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Read TKey
|
#region Read TKey
|
||||||
@@ -362,20 +344,6 @@ namespace MongoDbGenericRepository
|
|||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public virtual async Task<TValue> GetMinValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetMinValueAsync<TDocument, Guid, TValue>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -412,32 +380,6 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#region Utility Methods
|
#region Utility Methods
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a collections for the type TDocument with the matching partition key (if any).
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
|
||||||
protected virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a collections for the type TDocument
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="document">The document.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
if (document is IPartitionedDocument)
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
|
|
||||||
}
|
|
||||||
return GetCollection<TDocument>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collections for a potentially partitioned document type.
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -456,21 +398,6 @@ namespace MongoDbGenericRepository
|
|||||||
return GetCollection<TDocument, TKey>();
|
return GetCollection<TDocument, TKey>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a collections for a potentially partitioned document type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="partitionKey">The collection partition key.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(partitionKey))
|
|
||||||
{
|
|
||||||
return GetCollection<TDocument>(partitionKey);
|
|
||||||
}
|
|
||||||
return GetCollection<TDocument>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a collections for the type TDocument with a partition key.
|
/// Gets a collections for the type TDocument with a partition key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -503,20 +430,6 @@ namespace MongoDbGenericRepository
|
|||||||
return GetCollection<TDocument, TKey>();
|
return GetCollection<TDocument, TKey>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts a LINQ expression of TDocument, TValue to a LINQ expression of TDocument, object
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
||||||
/// <param name="expression">The expression to convert</param>
|
|
||||||
protected static Expression<Func<TDocument, object>> ConvertExpression<TDocument, TValue>(Expression<Func<TDocument, TValue>> expression)
|
|
||||||
{
|
|
||||||
var param = expression.Parameters[0];
|
|
||||||
Expression body = expression.Body;
|
|
||||||
var convert = Expression.Convert(body, typeof(object));
|
|
||||||
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,242 +1,11 @@
|
|||||||
using MongoDB.Driver;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using MongoDbGenericRepository.Models;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract partial class ReadOnlyMongoRepository : IReadOnlyMongoRepository
|
public abstract partial class ReadOnlyMongoRepository : KeyTypedReadOnlyMongoRepository<Guid>, IReadOnlyMongoRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
#region Read
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given its id.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="id">The Id of the document you want to get.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns one document given an expression filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection cursor.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var count = await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
|
|
||||||
return (count > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if any of the document of the collection matches the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
var count = HandlePartitioned<TDocument>(partitionKey).CountDocuments(filter);
|
|
||||||
return (count > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of the documents matching the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Counts how many documents match the filter condition.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
|
||||||
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).CountDocuments();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetByMaxAsync<TDocument, Guid>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetByMax<TDocument, Guid>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetByMinAsync<TDocument, Guid>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetByMin<TDocument, Guid>(filter, orderByAscending, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return await GetMaxValueAsync<TDocument, Guid, TValue>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
|
||||||
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetMaxValue<TDocument, Guid, TValue>(filter, maxValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
||||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
|
||||||
/// <param name="filter">A LINQ expression filter.</param>
|
|
||||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
|
||||||
public virtual TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
|
||||||
where TDocument : IDocument
|
|
||||||
{
|
|
||||||
return GetMinValue<TDocument, Guid, TValue>(filter, minValueSelector, partitionKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user