reorg + inheritance setup, using interface segregation to split concern / exposure of <TDocument,TKey> methods.
This commit is contained in:
@@ -7,17 +7,9 @@ using MongoDbGenericRepository.Models;
|
|||||||
|
|
||||||
namespace MongoDbGenericRepository
|
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
|
#region Read
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -199,5 +191,56 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion
|
#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;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using MongoDbGenericRepository.Models;
|
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
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 : 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>
|
/// </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 BaseMongoRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
|
protected BaseMongoRepository(string connectionString, string databaseName = null) : base(connectionString, databaseName)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -31,7 +30,6 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||||
protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||||
{
|
{
|
||||||
MongoDbContext = mongoDbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -40,7 +38,6 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||||
protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Create
|
#region Create
|
||||||
|
|||||||
+416
-8
@@ -9,37 +9,356 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
public interface IBaseReadOnlyRepository
|
||||||
/// 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
|
|
||||||
{
|
{
|
||||||
|
/// <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>
|
/// <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) : 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>
|
/// <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) : base(mongoDbContext)
|
protected BaseReadOnlyRepository(IMongoDbContext 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) : base(mongoDatabase)
|
protected BaseReadOnlyRepository(IMongoDatabase mongoDatabase)
|
||||||
{
|
{
|
||||||
|
MongoDbContext = new MongoDbContext(mongoDatabase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#region Read TKey
|
#region Read TKey
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -378,8 +697,83 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion
|
#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
|
#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>
|
/// <summary>
|
||||||
/// Gets a collections for a potentially partitioned document type.
|
/// Gets a collections for a potentially partitioned document type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -430,6 +824,20 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// </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 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
|
#region Create
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -129,7 +128,6 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion Create
|
#endregion Create
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the value of the document Id if it is not set already.
|
/// Sets the value of the document Id if it is not set already.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -149,6 +147,5 @@ namespace MongoDbGenericRepository
|
|||||||
document.Id = IdGenerator.GetId<TKey>();
|
document.Id = IdGenerator.GetId<TKey>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,55 +9,35 @@ 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> : 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>
|
/// <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 KeyTypedReadOnlyMongoRepository(string connectionString, string databaseName)
|
protected KeyTypedReadOnlyMongoRepository(string connectionString, string databaseName = null) : 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 KeyTypedReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
protected KeyTypedReadOnlyMongoRepository(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 KeyTypedReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
protected KeyTypedReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||||
{
|
{
|
||||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Read
|
#region Read
|
||||||
@@ -70,8 +50,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<TDocument> GetByIdAsync<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
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 base.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -82,8 +61,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
public TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
return base.GetById<TDocument, TKey>(id, partitionKey);
|
||||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -94,7 +72,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</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<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -105,7 +83,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -116,7 +94,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</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<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -127,8 +105,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</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<TKey>
|
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 await base.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||||
return (count > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -139,8 +116,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
var count = HandlePartitioned<TDocument>(partitionKey).CountDocuments(filter);
|
return base.Any<TDocument, TKey>(filter, partitionKey);
|
||||||
return (count > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -151,7 +127,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</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<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -162,7 +138,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partition key.</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<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -173,7 +149,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partitionKey</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<TKey>
|
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>
|
/// <summary>
|
||||||
@@ -184,7 +160,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="partitionKey">An optional partitionKey</param>
|
/// <param name="partitionKey">An optional partitionKey</param>
|
||||||
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<TKey>
|
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>
|
/// <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)
|
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 GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return await base.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
.SortByDescending(maxValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -214,10 +187,7 @@ 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 GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return base.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||||
.SortByDescending(maxValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
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 GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return await base.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
.SortBy(minValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -246,10 +213,7 @@ namespace MongoDbGenericRepository
|
|||||||
public TDocument GetByMin<TDocument>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, 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 GetCollection<TDocument>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
return base.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||||
.SortBy(minValueSelector)
|
|
||||||
.Limit(1)
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
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 GetMaxMongoQuery(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefaultAsync();
|
return await base.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
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 GetMaxMongoQuery(filter, maxValueSelector, partitionKey).Project(maxValueSelector).FirstOrDefault();
|
return base.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
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>
|
where TDocument : IDocument<TKey>
|
||||||
{
|
{
|
||||||
return await GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
return await base.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
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 GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
return base.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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
|
#region Utility Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -357,54 +385,6 @@ namespace MongoDbGenericRepository
|
|||||||
return GetCollection<TDocument>();
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net45;netstandard1.5;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>net45;netstandard2.0;netstandard1.5;</TargetFrameworks>
|
||||||
<PackageId>MongoDbGenericRepository</PackageId>
|
<PackageId>MongoDbGenericRepository</PackageId>
|
||||||
<PackageVersion>1.2.0</PackageVersion>
|
<PackageVersion>1.2.0</PackageVersion>
|
||||||
<Authors>Alexandre Spieser</Authors>
|
<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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user