reorg + inheritance setup, using interface segregation to split concern / exposure of <TDocument,TKey> methods.

This commit is contained in:
Alexandre SPIESER
2019-04-10 23:48:44 +01:00
parent 75b894cb2a
commit d2f465b063
9 changed files with 600 additions and 452 deletions
@@ -7,17 +7,9 @@ using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository
{
public interface IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
public interface IKeyTypedReadOnlyMongoRepository<TKey> : IBaseReadOnlyRepository
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>
@@ -199,5 +191,56 @@ namespace MongoDbGenericRepository
#endregion
#region Maths
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<int> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
int SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
decimal SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>;
#endregion Maths
}
}
@@ -1,273 +1,11 @@
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq.Expressions;
using MongoDbGenericRepository.Models;
using System;
namespace MongoDbGenericRepository
{
/// <summary>
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
/// </summary>
public interface IReadOnlyMongoRepository : IKeyTypedReadOnlyMongoRepository<Guid>
public interface IReadOnlyMongoRepository : IBaseReadOnlyRepository, IKeyTypedReadOnlyMongoRepository<Guid>
{
#region Read TKey
/// <summary>
/// Asynchronously returns one document given its id.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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>(TKey id, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns one document given its id.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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>(TKey id, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns one document given an expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns one document given an expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns a collection cursor.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns a list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns a list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously counts how many documents match the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partitionKey</param>
Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Counts how many documents match the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partitionKey</param>
long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="orderByAscending">A property selector to order by descending.</param>
/// <param name="partitionKey">An optional partitionKey.</param>
Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="orderByAscending">A property selector to order by descending.</param>
/// <param name="partitionKey">An optional partitionKey.</param>
TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets the document with the minimum value of a specified 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>
/// <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, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets the document with the minimum value of a specified 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>
/// <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, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
where TDocument : IDocument<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="TKey">The type of the primary key.</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<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<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="TKey">The type of the primary key.</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>
TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<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="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>
Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<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="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>
TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
#endregion
#region Sum
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
#endregion Sum
}
}
@@ -20,9 +20,8 @@ namespace MongoDbGenericRepository
/// </summary>
/// <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>
protected BaseMongoRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
protected BaseMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
{
MongoDbContext = new MongoDbContext(connectionString, databaseName);
}
/// <summary>
@@ -31,7 +30,6 @@ namespace MongoDbGenericRepository
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
MongoDbContext = mongoDbContext;
}
/// <summary>
@@ -40,7 +38,6 @@ namespace MongoDbGenericRepository
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
{
MongoDbContext = new MongoDbContext(mongoDatabase);
}
#region Create
@@ -9,37 +9,356 @@ using System.Threading.Tasks;
namespace MongoDbGenericRepository
{
/// <summary>
/// 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.
/// </summary>
public abstract partial class ReadOnlyMongoRepository : KeyTypedReadOnlyMongoRepository<Guid>, IReadOnlyMongoRepository
public interface IBaseReadOnlyRepository
{
/// <summary>
/// The connection string.
/// </summary>
string ConnectionString { get; }
/// <summary>
/// The database name.
/// </summary>
string DatabaseName { get; }
#region Read TKey
/// <summary>
/// Asynchronously returns one document given its id.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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>(TKey id, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns one document given its id.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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>(TKey id, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns one document given an expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns one document given an expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns a collection cursor.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns a list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns a list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously counts how many documents match the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partitionKey</param>
Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Counts how many documents match the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partitionKey</param>
long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="orderByAscending">A property selector to order by descending.</param>
/// <param name="partitionKey">An optional partitionKey.</param>
Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<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>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="orderByAscending">A property selector to order by descending.</param>
/// <param name="partitionKey">An optional partitionKey.</param>
TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets the document with the minimum value of a specified 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>
/// <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, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets the document with the minimum value of a specified 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>
/// <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, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> orderByAscending, string partitionKey = null)
where TDocument : IDocument<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="TKey">The type of the primary key.</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<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<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="TKey">The type of the primary key.</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>
TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> orderByDescending, string partitionKey = null)
where TDocument : IDocument<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="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>
Task<TValue> GetMinValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<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="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>
TValue GetMinValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
#endregion
#region Sum
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<int> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
int SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
decimal SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
#endregion Sum
}
public class BaseReadOnlyRepository : IBaseReadOnlyRepository
{
/// <summary>
/// The connection string.
/// </summary>
public string ConnectionString { get; }
/// <summary>
/// The database name.
/// </summary>
public string DatabaseName { get; }
/// <summary>
/// The MongoDbContext
/// </summary>
protected IMongoDbContext MongoDbContext = null;
/// <summary>
/// The constructor taking a connection string and a database name.
/// </summary>
/// <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>
protected ReadOnlyMongoRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
protected BaseReadOnlyRepository(string connectionString, string databaseName = null)
{
if (databaseName == null)
{
var mongoUrl = new MongoUrl(connectionString);
databaseName = databaseName ?? mongoUrl.DatabaseName;
}
ConnectionString = connectionString;
DatabaseName = 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 ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
protected BaseReadOnlyRepository(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 ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
protected BaseReadOnlyRepository(IMongoDatabase mongoDatabase)
{
MongoDbContext = new MongoDbContext(mongoDatabase);
}
#region Read TKey
/// <summary>
@@ -378,8 +697,83 @@ namespace MongoDbGenericRepository
#endregion
#region Sum TKey
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<int> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual int SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual decimal SumBy<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
}
#endregion Sums TKey
#region Utility Methods
protected IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return GetCollection<TDocument, TKey>(partitionKey).AsQueryable().Where(filter);
}
/// <summary>
/// Gets a collections for a potentially partitioned document type.
/// </summary>
@@ -430,6 +824,20 @@ namespace MongoDbGenericRepository
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
}
}
}
@@ -20,7 +20,7 @@ namespace MongoDbGenericRepository
/// </summary>
/// <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>
protected KeyTypedBaseMongoDbRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
protected KeyTypedBaseMongoDbRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
{
}
@@ -40,7 +40,6 @@ namespace MongoDbGenericRepository
{
}
#region Create
/// <summary>
@@ -129,7 +128,6 @@ namespace MongoDbGenericRepository
#endregion Create
/// <summary>
/// Sets the value of the document Id if it is not set already.
/// </summary>
@@ -149,6 +147,5 @@ namespace MongoDbGenericRepository
document.Id = IdGenerator.GetId<TKey>();
}
}
}
}
@@ -9,55 +9,35 @@ using System.Threading.Tasks;
namespace MongoDbGenericRepository
{
/// <summary>
/// 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.
/// </summary>
public abstract class KeyTypedReadOnlyMongoRepository<TKey> : IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
public abstract class KeyTypedReadOnlyMongoRepository<TKey> : BaseReadOnlyRepository, IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
{
/// <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>
/// The constructor taking a connection string and a database name.
/// </summary>
/// <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>
protected KeyTypedReadOnlyMongoRepository(string connectionString, string databaseName)
protected KeyTypedReadOnlyMongoRepository(string connectionString, string databaseName = null) : base(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)
protected KeyTypedReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(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)
protected KeyTypedReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
{
MongoDbContext = new MongoDbContext(mongoDatabase);
}
#region Read
@@ -70,8 +50,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param>
public async Task<TDocument> GetByIdAsync<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", id);
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
return await base.GetByIdAsync<TDocument, TKey>(id, partitionKey);
}
/// <summary>
@@ -82,8 +61,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param>
public TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", id);
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
return base.GetById<TDocument, TKey>(id, partitionKey);
}
/// <summary>
@@ -94,7 +72,7 @@ namespace MongoDbGenericRepository
/// <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<TKey>
{
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
return await base.GetOneAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -105,7 +83,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param>
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
{
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
return base.GetOne<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -116,7 +94,7 @@ namespace MongoDbGenericRepository
/// <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<TKey>
{
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
return base.GetCursor<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -127,8 +105,7 @@ namespace MongoDbGenericRepository
/// <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<TKey>
{
var count = await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
return (count > 0);
return await base.AnyAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -139,8 +116,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param>
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
{
var count = HandlePartitioned<TDocument>(partitionKey).CountDocuments(filter);
return (count > 0);
return base.Any<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -151,7 +127,7 @@ namespace MongoDbGenericRepository
/// <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<TKey>
{
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
return await base.GetAllAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -162,7 +138,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param>
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
{
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
return base.GetAll<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -173,7 +149,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partitionKey</param>
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
{
return await HandlePartitioned<TDocument>(partitionKey).CountDocumentsAsync(filter);
return await base.CountAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -184,7 +160,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partitionKey</param>
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
{
return HandlePartitioned<TDocument>(partitionKey).Find(filter).CountDocuments();
return base.Count<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
@@ -197,10 +173,7 @@ namespace MongoDbGenericRepository
public async Task<TDocument> GetByMaxAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
.SortByDescending(maxValueSelector)
.Limit(1)
.FirstOrDefaultAsync();
return await base.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
}
/// <summary>
@@ -214,10 +187,7 @@ namespace MongoDbGenericRepository
public TDocument GetByMax<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
.SortByDescending(maxValueSelector)
.Limit(1)
.FirstOrDefault();
return base.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
}
/// <summary>
@@ -230,10 +200,7 @@ namespace MongoDbGenericRepository
public async Task<TDocument> GetByMinAsync<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
.SortBy(minValueSelector)
.Limit(1)
.FirstOrDefaultAsync();
return await base.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
}
/// <summary>
@@ -246,10 +213,7 @@ namespace MongoDbGenericRepository
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
.SortBy(minValueSelector)
.Limit(1)
.FirstOrDefault();
return base.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
}
/// <summary>
@@ -263,7 +227,7 @@ namespace MongoDbGenericRepository
public async Task<TValue> GetMaxValueAsync<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await GetMaxMongoQuery(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefaultAsync();
return await base.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
}
/// <summary>
@@ -277,7 +241,7 @@ namespace MongoDbGenericRepository
public TValue GetMaxValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return GetMaxMongoQuery(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefault();
return base.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
}
/// <summary>
@@ -291,7 +255,7 @@ namespace MongoDbGenericRepository
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(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
return await base.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
}
/// <summary>
@@ -305,11 +269,75 @@ namespace MongoDbGenericRepository
public virtual TValue GetMinValue<TDocument, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
return base.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
}
#endregion
#region Maths
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<int> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await base.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await base.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual int SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, int>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return base.SumBy<TDocument, TKey>(filter, selector, partitionKey);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual decimal SumBy<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return base.SumBy<TDocument, TKey>(filter, selector, partitionKey);
}
#endregion Maths
#region Utility Methods
/// <summary>
@@ -357,54 +385,6 @@ namespace MongoDbGenericRepository
return GetCollection<TDocument>();
}
/// <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);
}
/// <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
}
}
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard1.5;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard2.0;netstandard1.5;</TargetFrameworks>
<PackageId>MongoDbGenericRepository</PackageId>
<PackageVersion>1.2.0</PackageVersion>
<Authors>Alexandre Spieser</Authors>
@@ -1,52 +0,0 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDbGenericRepository.Models;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MongoDbGenericRepository
{
/// <summary>
/// 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.
/// </summary>
public abstract partial class ReadOnlyMongoRepository
{
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<decimal> SumByAsync<TDocument>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument
{
return await SumByAsync<TDocument, Guid>(filter, selector, partitionKey);
}
/// <summary>
/// Sums the values of a selected field for a given filtered collection of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param>
public virtual async Task<decimal> SumByAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, decimal>> selector,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await GetCollection<TDocument, TKey>(partitionKey)
.AsQueryable()
.Where(filter)
.SumAsync(selector);
}
}
}
@@ -0,0 +1,37 @@
using MongoDB.Driver;
using System;
namespace MongoDbGenericRepository
{
/// <summary>
/// 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.
/// </summary>
public abstract class ReadOnlyMongoRepository : KeyTypedReadOnlyMongoRepository<Guid>, IReadOnlyMongoRepository
{
/// <summary>
/// The constructor taking a connection string and a database name.
/// </summary>
/// <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>
protected ReadOnlyMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
{
}
/// <summary>
/// The contructor taking a <see cref="IMongoDbContext"/>.
/// </summary>
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
/// <summary>
/// The contructor taking a <see cref="IMongoDatabase"/>.
/// </summary>
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
{
}
}
}