first step to split function into separate classes for better reuse and easier maintenance.
This commit is contained in:
@@ -1119,6 +1119,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
#endregion Math
|
||||
|
||||
#region Test Utils
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetCurrentMethod()
|
||||
{
|
||||
|
||||
@@ -7,8 +7,7 @@ using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
public interface IKeyTypedReadOnlyMongoRepository<TKey> : IBaseReadOnlyRepository
|
||||
where TKey : IEquatable<TKey>
|
||||
public interface IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
#region Read
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace MongoDbGenericRepository
|
||||
/// <summary>
|
||||
/// The repository interface for managing indexes
|
||||
/// </summary>
|
||||
public interface IMongoDbCollectionIndexRepository
|
||||
public interface IMongoDbCollectionIndexRepository : IBaseMongoDbIndexRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a text index on the given field.
|
||||
@@ -24,21 +24,6 @@ namespace MongoDbGenericRepository
|
||||
Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Create a text index on the given field.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
@@ -51,21 +36,6 @@ namespace MongoDbGenericRepository
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in descending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
@@ -79,21 +49,6 @@ namespace MongoDbGenericRepository
|
||||
Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in descending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hashed index on the given field.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
@@ -107,21 +62,6 @@ namespace MongoDbGenericRepository
|
||||
Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hashed index on the given field.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a combined text index.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
@@ -135,21 +75,6 @@ namespace MongoDbGenericRepository
|
||||
Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a combined text index.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="fields">The fields we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Drops the index given a field name
|
||||
/// </summary>
|
||||
@@ -159,17 +84,6 @@ namespace MongoDbGenericRepository
|
||||
Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Drops the index given a field name
|
||||
/// </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="indexName">The name of the index</param>
|
||||
/// <param name="partitionKey">An optional partition key</param>
|
||||
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the names of the indexes present on a collection.
|
||||
/// </summary>
|
||||
@@ -178,16 +92,5 @@ namespace MongoDbGenericRepository
|
||||
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||
Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the names of the indexes present on a collection.
|
||||
/// </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="partitionKey">An optional partition key</param>
|
||||
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
public interface IBaseMongoDbIndexRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a text index on the given field.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in descending order.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hashed index on the given field.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="field">The field we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a combined text index.
|
||||
/// IndexCreationOptions can be supplied to further specify
|
||||
/// how the creation should be done.
|
||||
/// </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="fields">The fields we want to index.</param>
|
||||
/// <param name="indexCreationOptions">Options for creating an index.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Drops the index given a field name
|
||||
/// </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="indexName">The name of the index</param>
|
||||
/// <param name="partitionKey">An optional partition key</param>
|
||||
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the names of the indexes present on a collection.
|
||||
/// </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="partitionKey">An optional partition key</param>
|
||||
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
|
||||
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
|
||||
public class BaseMongoDbIndexRepository : BaseReadOnlyRepository, IBaseMongoDbIndexRepository
|
||||
{
|
||||
/// <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 BaseMongoDbIndexRepository(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 BaseMongoDbIndexRepository(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 BaseMongoDbIndexRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
#region Index Management TKey
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
|
||||
var indexes = await indexCursor.ToListAsync();
|
||||
return indexes.Select(e => e["name"].ToString()).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(
|
||||
Builders<TDocument>.IndexKeys.Text(field),
|
||||
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
||||
));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
|
||||
foreach (var field in fields)
|
||||
{
|
||||
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
|
||||
}
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async virtual Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
|
||||
}
|
||||
|
||||
#endregion Index Management TKey
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,38 +23,12 @@ namespace MongoDbGenericRepository
|
||||
return await CreateTextIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(
|
||||
Builders<TDocument>.IndexKeys.Text(field),
|
||||
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
||||
));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await CreateAscendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
@@ -62,18 +36,6 @@ namespace MongoDbGenericRepository
|
||||
return await CreateDescendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
@@ -82,40 +44,12 @@ namespace MongoDbGenericRepository
|
||||
return await CreateHashedIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await CreateCombinedTextIndexAsync<TDocument, Guid>(fields, indexCreationOptions, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
|
||||
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
|
||||
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
|
||||
foreach (var field in fields)
|
||||
{
|
||||
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
|
||||
}
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
|
||||
@@ -124,14 +58,6 @@ namespace MongoDbGenericRepository
|
||||
await DropIndexAsync<TDocument, Guid>(indexName, partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
@@ -139,38 +65,8 @@ namespace MongoDbGenericRepository
|
||||
return await GetIndexesNamesAsync<TDocument, Guid>(partitionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
|
||||
var indexes = await indexCursor.ToListAsync();
|
||||
return indexes.Select(e => e["name"].ToString()).ToList();
|
||||
}
|
||||
|
||||
|
||||
#endregion Index Management
|
||||
|
||||
private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
||||
{
|
||||
return new CreateIndexOptions
|
||||
{
|
||||
Unique = indexCreationOptions.Unique,
|
||||
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
||||
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
||||
Sparse = indexCreationOptions.Sparse,
|
||||
Name = indexCreationOptions.Name,
|
||||
Min = indexCreationOptions.Min,
|
||||
Max = indexCreationOptions.Max,
|
||||
LanguageOverride = indexCreationOptions.LanguageOverride,
|
||||
ExpireAfter = indexCreationOptions.ExpireAfter,
|
||||
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
||||
BucketSize = indexCreationOptions.BucketSize,
|
||||
Bits = indexCreationOptions.Bits,
|
||||
Background = indexCreationOptions.Background,
|
||||
Version = indexCreationOptions.Version
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System.Linq;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
@@ -15,6 +17,45 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
|
||||
{
|
||||
private object _initLock;
|
||||
private MongoDbCreator _mongoDbCreator;
|
||||
protected MongoDbCreator MongoDbCreator
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_mongoDbCreator != null) { return _mongoDbCreator; }
|
||||
|
||||
lock (_initLock)
|
||||
{
|
||||
if (_mongoDbCreator == null)
|
||||
{
|
||||
_mongoDbCreator = new MongoDbCreator(MongoDbContext);
|
||||
}
|
||||
}
|
||||
|
||||
return _mongoDbCreator;
|
||||
}
|
||||
}
|
||||
|
||||
private MongoDbUpdater _mongoDbUpdater;
|
||||
protected MongoDbUpdater MongoDbUpdater
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_mongoDbUpdater != null) { return _mongoDbUpdater; }
|
||||
|
||||
lock (_initLock)
|
||||
{
|
||||
if (_mongoDbUpdater == null)
|
||||
{
|
||||
_mongoDbUpdater = new MongoDbUpdater(MongoDbContext);
|
||||
}
|
||||
}
|
||||
|
||||
return _mongoDbUpdater;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor taking a connection string and a database name.
|
||||
/// </summary>
|
||||
@@ -40,94 +81,6 @@ namespace MongoDbGenericRepository
|
||||
{
|
||||
}
|
||||
|
||||
#region Create
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public virtual async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
FormatDocument(document);
|
||||
await HandlePartitioned(document).InsertOneAsync(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
FormatDocument(document);
|
||||
HandlePartitioned(document).InsertOne(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await GetCollection<TDocument>().InsertManyAsync(documents.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
HandlePartitioned(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetCollection<TDocument>().InsertMany(documents.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Create
|
||||
|
||||
#region Create TKey
|
||||
|
||||
/// <summary>
|
||||
@@ -141,8 +94,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document);
|
||||
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -156,8 +108,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
HandlePartitioned<TDocument, TKey>(document).InsertOne(document);
|
||||
MongoDbCreator.AddOne<TDocument, TKey>(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -171,26 +122,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await GetCollection<TDocument, TKey>().InsertManyAsync(documents.ToList());
|
||||
}
|
||||
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -204,26 +136,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetCollection<TDocument, TKey>().InsertMany(documents.ToList());
|
||||
}
|
||||
MongoDbCreator.AddMany<TDocument, TKey>(documents);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -237,8 +150,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -248,8 +160,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, Guid>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -261,9 +172,20 @@ namespace MongoDbGenericRepository
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, update);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid>(documentToModify, update);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, Guid>(documentToModify, update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -277,9 +199,7 @@ namespace MongoDbGenericRepository
|
||||
public virtual bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -293,9 +213,7 @@ namespace MongoDbGenericRepository
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -310,9 +228,7 @@ namespace MongoDbGenericRepository
|
||||
public virtual bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
|
||||
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -327,9 +243,7 @@ namespace MongoDbGenericRepository
|
||||
public virtual bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
|
||||
var updateRes = collection.UpdateOne(Builders<TDocument>.Filter.Where(filter), Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -344,9 +258,7 @@ namespace MongoDbGenericRepository
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
|
||||
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -361,24 +273,9 @@ namespace MongoDbGenericRepository
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
|
||||
var updateRes = await collection.UpdateOneAsync(Builders<TDocument>.Filter.Where(filter), Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, Guid, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
@@ -394,9 +291,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -409,9 +304,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(filter, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -425,9 +318,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(documentToModify, update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -441,27 +332,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey>(documentToModify, update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -477,9 +348,57 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -496,9 +415,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
||||
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -518,42 +435,6 @@ namespace MongoDbGenericRepository
|
||||
return await UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
||||
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return UpdateOne<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
@@ -1055,9 +936,9 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public virtual async Task<List<TDocument>> GetPaginatedAsync<TDocument>(
|
||||
Expression<Func<TDocument, bool>> filter,
|
||||
int skipNumber = 0,
|
||||
int takeNumber = 50,
|
||||
Expression<Func<TDocument, bool>> filter,
|
||||
int skipNumber = 0,
|
||||
int takeNumber = 50,
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
{
|
||||
|
||||
+121
-103
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -323,6 +324,11 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
protected IMongoDbContext MongoDbContext = null;
|
||||
|
||||
/// <summary>
|
||||
/// A MongoDb Reader for read operations
|
||||
/// </summary>
|
||||
protected MongoDbReader MongoDbReader = null;
|
||||
|
||||
/// <summary>
|
||||
/// The constructor taking a connection string and a database name.
|
||||
/// </summary>
|
||||
@@ -337,7 +343,7 @@ namespace MongoDbGenericRepository
|
||||
}
|
||||
ConnectionString = connectionString;
|
||||
DatabaseName = databaseName;
|
||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
||||
SetupMongoDbContext(new MongoDbContext(connectionString, databaseName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -346,7 +352,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||
protected BaseReadOnlyRepository(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
MongoDbContext = mongoDbContext;
|
||||
SetupMongoDbContext(mongoDbContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -355,9 +361,14 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||
protected BaseReadOnlyRepository(IMongoDatabase mongoDatabase)
|
||||
{
|
||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
||||
SetupMongoDbContext(new MongoDbContext(mongoDatabase));
|
||||
}
|
||||
|
||||
protected void SetupMongoDbContext(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
MongoDbContext = mongoDbContext;
|
||||
MongoDbReader = new MongoDbReader(MongoDbContext);
|
||||
}
|
||||
|
||||
#region Read TKey
|
||||
|
||||
@@ -368,12 +379,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public async Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
public async virtual Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -383,12 +393,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
public virtual TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -398,11 +407,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public async Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public async virtual Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -412,11 +421,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public virtual TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
return MongoDbReader.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -426,11 +435,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public virtual IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter);
|
||||
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -440,12 +449,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public async Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public async virtual Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||
return (count > 0);
|
||||
return await MongoDbReader.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -455,12 +463,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public virtual bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(filter);
|
||||
return (count > 0);
|
||||
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -470,11 +477,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public async Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public async virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToListAsync();
|
||||
return await MongoDbReader.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -484,11 +491,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public virtual List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList();
|
||||
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -498,11 +505,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public async Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public async virtual Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||
return await MongoDbReader.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -512,11 +519,11 @@ namespace MongoDbGenericRepository
|
||||
/// <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>
|
||||
public long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
public virtual long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).CountDocuments();
|
||||
return MongoDbReader.Count<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -527,14 +534,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(maxValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -545,14 +549,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
public virtual TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(maxValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefault();
|
||||
return MongoDbReader.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -563,14 +564,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
public async virtual Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortBy(minValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -581,50 +579,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
public virtual TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortBy(minValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(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, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(ConvertExpression(maxValueSelector))
|
||||
.Limit(1);
|
||||
return MongoDbReader.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -635,13 +594,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="orderByAscending">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async Task<TValue> GetMaxValueAsync<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
public async virtual 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>
|
||||
{
|
||||
return await GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||
.Project(maxValueSelector)
|
||||
.FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -653,14 +610,11 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
public virtual TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
|
||||
return GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||
.Project(maxValueSelector)
|
||||
.FirstOrDefault();
|
||||
return MongoDbReader.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -676,7 +630,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
||||
return await MongoDbReader.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -692,7 +646,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
||||
return MongoDbReader.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -712,7 +666,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
|
||||
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -728,7 +682,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
|
||||
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -744,7 +698,7 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetQuery<TDocument, TKey>(filter, partitionKey).SumAsync(selector);
|
||||
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -760,14 +714,14 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetQuery<TDocument, TKey>(filter, partitionKey).Sum(selector);
|
||||
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
#endregion Sums TKey
|
||||
#endregion Sum TKey
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
protected IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
protected virtual IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
@@ -830,7 +784,7 @@ namespace MongoDbGenericRepository
|
||||
/// <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)
|
||||
protected virtual Expression<Func<TDocument, object>> ConvertExpression<TDocument, TValue>(Expression<Func<TDocument, TValue>> expression)
|
||||
{
|
||||
var param = expression.Parameters[0];
|
||||
Expression body = expression.Body;
|
||||
@@ -838,6 +792,70 @@ namespace MongoDbGenericRepository
|
||||
return Expression.Lambda<Func<TDocument, object>>(convert, param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a IndexCreationOptions object to a MongoDB.Driver.CreateIndexOptions object
|
||||
/// </summary>
|
||||
/// <param name="indexCreationOptions">The options for creating an index.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
||||
{
|
||||
return new CreateIndexOptions
|
||||
{
|
||||
Unique = indexCreationOptions.Unique,
|
||||
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
||||
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
||||
Sparse = indexCreationOptions.Sparse,
|
||||
Name = indexCreationOptions.Name,
|
||||
Min = indexCreationOptions.Min,
|
||||
Max = indexCreationOptions.Max,
|
||||
LanguageOverride = indexCreationOptions.LanguageOverride,
|
||||
ExpireAfter = indexCreationOptions.ExpireAfter,
|
||||
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
||||
BucketSize = indexCreationOptions.BucketSize,
|
||||
Bits = indexCreationOptions.Bits,
|
||||
Background = indexCreationOptions.Background,
|
||||
Version = indexCreationOptions.Version
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <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>
|
||||
protected virtual IFindFluent<TDocument, TDocument> GetMinMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(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>
|
||||
protected virtual IFindFluent<TDocument, TDocument> GetMaxMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(ConvertExpression(maxValueSelector))
|
||||
.Limit(1);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Base
|
||||
{
|
||||
public class DataAccessBase
|
||||
{
|
||||
protected IMongoDbContext MongoDbContext;
|
||||
|
||||
public DataAccessBase(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
MongoDbContext = mongoDbContext;
|
||||
}
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
protected virtual 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>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (document is IPartitionedDocument)
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(((IPartitionedDocument)document).PartitionKey);
|
||||
}
|
||||
return GetCollection<TDocument, TKey>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument with a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey);
|
||||
}
|
||||
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 virtual 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>
|
||||
/// Maps a IndexCreationOptions object to a MongoDB.Driver.CreateIndexOptions object
|
||||
/// </summary>
|
||||
/// <param name="indexCreationOptions">The options for creating an index.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
|
||||
{
|
||||
return new CreateIndexOptions
|
||||
{
|
||||
Unique = indexCreationOptions.Unique,
|
||||
TextIndexVersion = indexCreationOptions.TextIndexVersion,
|
||||
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
|
||||
Sparse = indexCreationOptions.Sparse,
|
||||
Name = indexCreationOptions.Name,
|
||||
Min = indexCreationOptions.Min,
|
||||
Max = indexCreationOptions.Max,
|
||||
LanguageOverride = indexCreationOptions.LanguageOverride,
|
||||
ExpireAfter = indexCreationOptions.ExpireAfter,
|
||||
DefaultLanguage = indexCreationOptions.DefaultLanguage,
|
||||
BucketSize = indexCreationOptions.BucketSize,
|
||||
Bits = indexCreationOptions.Bits,
|
||||
Background = indexCreationOptions.Background,
|
||||
Version = indexCreationOptions.Version
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <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>
|
||||
protected virtual IFindFluent<TDocument, TDocument> GetMinMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(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>
|
||||
protected virtual IFindFluent<TDocument, TDocument> GetMaxMongoQuery<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(ConvertExpression(maxValueSelector))
|
||||
.Limit(1);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Create
|
||||
{
|
||||
public class MongoDbCreator : DataAccessBase
|
||||
{
|
||||
public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
#region Create TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="document">The document you want to add.</param>
|
||||
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="document">The document you want to add.</param>
|
||||
public virtual void AddOne<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
HandlePartitioned<TDocument, TKey>(document).InsertOne(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="documents">The documents you want to add.</param>
|
||||
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await GetCollection<TDocument, TKey>().InsertManyAsync(documents.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="documents">The documents you want to add.</param>
|
||||
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetCollection<TDocument, TKey>().InsertMany(documents.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
protected void FormatDocument<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
var defaultTKey = default(TKey);
|
||||
if (document.Id == null
|
||||
|| (defaultTKey != null
|
||||
&& defaultTKey.Equals(document.Id)))
|
||||
{
|
||||
document.Id = IdGenerator.GetId<TKey>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
{
|
||||
public class MongoDbEraser : DataAccessBase
|
||||
{
|
||||
public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
#region Delete TKey
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document.
|
||||
/// </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="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual long DeleteOne<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
|
||||
return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter).DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document matching the condition of the LINQ 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="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
|
||||
return (await HandlePartitioned<TDocument, TKey>(document).DeleteOneAsync(filter)).DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document matching the condition of the LINQ 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>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter).DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document matching the condition of the LINQ 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>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOneAsync(filter)).DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes the documents matching the condition of the LINQ 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>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a list of documents.
|
||||
/// </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="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
long deleteCount = 0;
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
|
||||
deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
|
||||
}
|
||||
return deleteCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||
return (await HandlePartitioned<TDocument, TKey>(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a list of documents.
|
||||
/// </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="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
long deleteCount = 0;
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
|
||||
deleteCount += (HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
|
||||
}
|
||||
return deleteCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||
return (HandlePartitioned<TDocument, TKey>(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id))).DeletedCount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the documents matching the condition of the LINQ 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>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter).DeletedCount;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Read
|
||||
{
|
||||
public class MongoDbReader : DataAccessBase
|
||||
{
|
||||
public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
#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>
|
||||
public async virtual Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <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>
|
||||
public async virtual Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <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>
|
||||
public virtual bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = HandlePartitioned<TDocument, TKey>(partitionKey).CountDocuments(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <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>
|
||||
public async virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <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>
|
||||
public virtual List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).CountDocumentsAsync(filter);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).CountDocuments();
|
||||
}
|
||||
|
||||
#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="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(maxValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortByDescending(maxValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <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="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async virtual Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortBy(minValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey).Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.SortBy(minValueSelector)
|
||||
.Limit(1)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual 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>
|
||||
{
|
||||
return await GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||
.Project(maxValueSelector)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <typeparam name="TValue">The type of the value used to order the query.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return GetMaxMongoQuery<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey)
|
||||
.Project(maxValueSelector)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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>
|
||||
public virtual async 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>
|
||||
{
|
||||
return await GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual 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>
|
||||
{
|
||||
return GetMinMongoQuery<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault();
|
||||
}
|
||||
|
||||
|
||||
#endregion Min / Max
|
||||
|
||||
#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 Sum TKey
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Update
|
||||
{
|
||||
public class MongoDbUpdater : DataAccessBase
|
||||
{
|
||||
public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously Updates a document.
|
||||
/// </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="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document.
|
||||
/// </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="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(filter, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </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="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </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="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
|
||||
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
||||
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
|
||||
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return UpdateOne<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
@@ -15,6 +16,45 @@ namespace MongoDbGenericRepository
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
public abstract class KeyTypedBaseMongoDbRepository<TKey> : KeyTypedReadOnlyMongoRepository<TKey>, IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
protected object _initLock;
|
||||
protected MongoDbCreator _mongoDbCreator;
|
||||
protected MongoDbCreator MongoDbCreator
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_mongoDbCreator == null)
|
||||
{
|
||||
lock (_initLock)
|
||||
{
|
||||
if(_mongoDbCreator == null)
|
||||
{
|
||||
_mongoDbCreator = new MongoDbCreator(MongoDbContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _mongoDbCreator;
|
||||
}
|
||||
}
|
||||
|
||||
private MongoDbUpdater _mongoDbUpdater;
|
||||
protected MongoDbUpdater MongoDbUpdater
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_mongoDbUpdater != null) { return _mongoDbUpdater; }
|
||||
|
||||
lock (_initLock)
|
||||
{
|
||||
if (_mongoDbUpdater == null)
|
||||
{
|
||||
_mongoDbUpdater = new MongoDbUpdater(MongoDbContext);
|
||||
}
|
||||
}
|
||||
|
||||
return _mongoDbUpdater;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor taking a connection string and a database name.
|
||||
/// </summary>
|
||||
@@ -50,8 +90,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public virtual async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument<TKey>
|
||||
{
|
||||
FormatDocument(document);
|
||||
await HandlePartitioned(document).InsertOneAsync(document);
|
||||
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -62,8 +101,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument<TKey>
|
||||
{
|
||||
FormatDocument(document);
|
||||
HandlePartitioned(document).InsertOne(document);
|
||||
MongoDbCreator.AddOne<TDocument, TKey>(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -74,26 +112,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await GetCollection<TDocument>().InsertManyAsync(documents.ToList());
|
||||
}
|
||||
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -104,48 +123,146 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documents.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
{
|
||||
HandlePartitioned(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetCollection<TDocument>().InsertMany(documents.ToList());
|
||||
}
|
||||
MongoDbCreator.AddMany<TDocument, TKey>(documents);
|
||||
}
|
||||
|
||||
#endregion Create
|
||||
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
protected void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument<TKey>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
var defaultTKey = default(TKey);
|
||||
if (document.Id == null
|
||||
|| (defaultTKey != null
|
||||
&& defaultTKey.Equals(document.Id)))
|
||||
{
|
||||
document.Id = IdGenerator.GetId<TKey>();
|
||||
}
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public virtual bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey>(modifiedDocument);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey>(documentToModify, update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey>(documentToModify, update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(documentToModify, field, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbUpdater.UpdateOne<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await MongoDbUpdater.UpdateOneAsync<TDocument, TKey, TField>(filter, field, value, partitionKey);
|
||||
}
|
||||
|
||||
|
||||
#endregion Update
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -13,14 +12,43 @@ namespace MongoDbGenericRepository
|
||||
/// 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> : BaseReadOnlyRepository, IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||
public abstract class KeyTypedReadOnlyMongoRepository<TKey> : IKeyTypedReadOnlyMongoRepository<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// The connection string.
|
||||
/// </summary>
|
||||
public string ConnectionString { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The database name.
|
||||
/// </summary>
|
||||
public string DatabaseName { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The MongoDbContext
|
||||
/// </summary>
|
||||
protected IMongoDbContext MongoDbContext = null;
|
||||
|
||||
/// <summary>
|
||||
/// A MongoDb Reader for read operations
|
||||
/// </summary>
|
||||
protected MongoDbReader MongoDbReader = 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 = null) : base(connectionString, databaseName)
|
||||
protected KeyTypedReadOnlyMongoRepository(string connectionString, string databaseName = null)
|
||||
{
|
||||
SetupMongoDbContext(connectionString, databaseName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||
/// </summary>
|
||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||
protected KeyTypedReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : this(new MongoDbContext(mongoDatabase))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -28,16 +56,27 @@ namespace MongoDbGenericRepository
|
||||
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||
protected KeyTypedReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
protected KeyTypedReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
SetupMongoDbContext(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) : base(mongoDatabase)
|
||||
protected void SetupMongoDbContext(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
MongoDbContext = MongoDbContext ?? mongoDbContext;
|
||||
MongoDbReader = MongoDbReader ?? new MongoDbReader(MongoDbContext);
|
||||
}
|
||||
|
||||
protected void SetupMongoDbContext(string connectionString, string databaseName)
|
||||
{
|
||||
if (databaseName == null)
|
||||
{
|
||||
var mongoUrl = new MongoUrl(connectionString);
|
||||
databaseName = databaseName ?? mongoUrl.DatabaseName;
|
||||
}
|
||||
ConnectionString = connectionString;
|
||||
DatabaseName = databaseName;
|
||||
SetupMongoDbContext(new MongoDbContext(connectionString, databaseName));
|
||||
}
|
||||
|
||||
#region Read
|
||||
@@ -50,7 +89,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>
|
||||
{
|
||||
return await base.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||
return await MongoDbReader.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +100,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetById<TDocument>(TKey id, string partitionKey = null) where TDocument : IDocument<TKey>
|
||||
{
|
||||
return base.GetById<TDocument, TKey>(id, partitionKey);
|
||||
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -72,7 +111,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 base.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||
return await MongoDbReader.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -83,7 +122,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 base.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||
return MongoDbReader.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -94,7 +133,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 base.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -105,7 +144,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>
|
||||
{
|
||||
return await base.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||
return await MongoDbReader.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -116,7 +155,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>
|
||||
{
|
||||
return base.Any<TDocument, TKey>(filter, partitionKey);
|
||||
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -127,7 +166,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 base.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||
return await MongoDbReader.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -138,7 +177,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 base.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -149,7 +188,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 base.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||
return await MongoDbReader.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -160,7 +199,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 base.Count<TDocument, TKey>(filter, partitionKey);
|
||||
return MongoDbReader.Count<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -173,7 +212,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 base.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
return await MongoDbReader.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -187,7 +226,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 base.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
return MongoDbReader.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -200,7 +239,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 base.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
return await MongoDbReader.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -213,7 +252,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 base.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
return MongoDbReader.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -227,7 +266,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 base.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
return await MongoDbReader.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -241,7 +280,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 base.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
return MongoDbReader.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -255,7 +294,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 base.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
return await MongoDbReader.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -269,7 +308,7 @@ 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 base.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
return MongoDbReader.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -288,7 +327,7 @@ namespace MongoDbGenericRepository
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await base.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -303,7 +342,7 @@ namespace MongoDbGenericRepository
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return await base.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
return await MongoDbReader.SumByAsync<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -318,7 +357,7 @@ namespace MongoDbGenericRepository
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return base.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -333,58 +372,9 @@ namespace MongoDbGenericRepository
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
return base.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
return MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
#endregion Maths
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument with the matching partition key (if any).
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
||||
protected virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument<TKey>
|
||||
{
|
||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
if (document is IPartitionedDocument)
|
||||
{
|
||||
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
|
||||
}
|
||||
return GetCollection<TDocument>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected virtual IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey)
|
||||
where TDocument : IDocument<TKey>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return GetCollection<TDocument>(partitionKey);
|
||||
}
|
||||
return GetCollection<TDocument>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -21,24 +21,6 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
public IMongoDatabase Database { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Guid representation of the MongoDB Driver.
|
||||
/// </summary>
|
||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||
{
|
||||
MongoDefaults.GuidRepresentation = guidRepresentation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the Guid representation of the MongoDB Driver.
|
||||
/// Override this method to change the default GuidRepresentation.
|
||||
/// </summary>
|
||||
protected virtual void InitializeGuidRepresentation()
|
||||
{
|
||||
// by default, avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
|
||||
@@ -76,24 +58,12 @@ namespace MongoDbGenericRepository
|
||||
Database = client.GetDatabase(databaseName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the CollectionName attribute from the entity type, if any.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <returns>The name of the collection in which the TDocument is stored.</returns>
|
||||
private string GetAttributeCollectionName<TDocument>()
|
||||
{
|
||||
return (typeof(TDocument).GetTypeInfo()
|
||||
.GetCustomAttributes(typeof(CollectionNameAttribute))
|
||||
.FirstOrDefault() as CollectionNameAttribute)?.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection for a document type. Also handles document types with a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="partitionKey">The optional value of the partition key.</param>
|
||||
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||
public virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||
{
|
||||
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
|
||||
}
|
||||
@@ -102,18 +72,49 @@ namespace MongoDbGenericRepository
|
||||
/// Drops a collection, use very carefully.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
public void DropCollection<TDocument>(string partitionKey = null)
|
||||
public virtual void DropCollection<TDocument>(string partitionKey = null)
|
||||
{
|
||||
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Guid representation of the MongoDB Driver.
|
||||
/// </summary>
|
||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||
{
|
||||
MongoDefaults.GuidRepresentation = guidRepresentation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the CollectionName attribute from the entity type, if any.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <returns>The name of the collection in which the TDocument is stored.</returns>
|
||||
protected virtual string GetAttributeCollectionName<TDocument>()
|
||||
{
|
||||
return (typeof(TDocument).GetTypeInfo()
|
||||
.GetCustomAttributes(typeof(CollectionNameAttribute))
|
||||
.FirstOrDefault() as CollectionNameAttribute)?.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the Guid representation of the MongoDB Driver.
|
||||
/// Override this method to change the default GuidRepresentation.
|
||||
/// </summary>
|
||||
protected virtual void InitializeGuidRepresentation()
|
||||
{
|
||||
// by default, avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given the document type and the partition key, returns the name of the collection it belongs to.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
/// <returns>The name of the collection.</returns>
|
||||
private string GetCollectionName<TDocument>(string partitionKey)
|
||||
protected virtual string GetCollectionName<TDocument>(string partitionKey)
|
||||
{
|
||||
var collectionName = GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
|
||||
if (string.IsNullOrEmpty(partitionKey))
|
||||
@@ -128,7 +129,7 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <returns>The pluralized document name.</returns>
|
||||
private string Pluralize<TDocument>()
|
||||
protected virtual string Pluralize<TDocument>()
|
||||
{
|
||||
return (typeof(TDocument).Name.Pluralize()).Camelize();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
@@ -33,5 +38,356 @@ namespace MongoDbGenericRepository
|
||||
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
#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>
|
||||
public async virtual Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.GetByIdAsync<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetById<TDocument, TKey>(id, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.GetOneAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetOne<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetCursor<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.AnyAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.Any<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.GetAllAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetAll<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public async virtual Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.CountAsync<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.Count<TDocument, TKey>(filter, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.GetByMaxAsync<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="maxValueSelector">A property selector to order by descending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TDocument GetByMax<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetByMax<TDocument, TKey>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <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="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public async virtual Task<TDocument> GetByMinAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await MongoDbReader.GetByMinAsync<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <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="minValueSelector">A property selector to order by ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TDocument GetByMin<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> minValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetByMin<TDocument, TKey>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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>
|
||||
public async virtual 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>
|
||||
{
|
||||
return await MongoDbReader.GetMaxValueAsync<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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 ascending.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey.</param>
|
||||
public virtual TValue GetMaxValue<TDocument, TKey, TValue>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TValue>> maxValueSelector, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.GetMaxValue<TDocument, TKey, TValue>(filter, maxValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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>
|
||||
public virtual async 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>
|
||||
{
|
||||
return await MongoDbReader.GetMinValueAsync<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum value of a property in a mongodb collections that is satisfying the filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="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>
|
||||
public virtual 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>
|
||||
{
|
||||
return MongoDbReader.GetMinValue<TDocument, TKey, TValue>(filter, minValueSelector, partitionKey);
|
||||
}
|
||||
|
||||
#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 MongoDbReader.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, TKey>(Expression<Func<TDocument, bool>> filter,
|
||||
Expression<Func<TDocument, int>> selector,
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbReader.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 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 MongoDbReader.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 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 MongoDbReader.SumBy<TDocument, TKey>(filter, selector, partitionKey);
|
||||
}
|
||||
|
||||
#endregion Sum TKey
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user