470 lines
22 KiB
C#
470 lines
22 KiB
C#
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Linq.Expressions;
|
|
using MongoDbGenericRepository.Models;
|
|
using System.Linq;
|
|
|
|
namespace MongoDbGenericRepository
|
|
{
|
|
/// <summary>
|
|
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
|
/// </summary>
|
|
public class ReadOnlyMongoRepository : IReadOnlyMongoRepository
|
|
{
|
|
/// <summary>
|
|
/// The connection string.
|
|
/// </summary>
|
|
public string ConnectionString { get; set; }
|
|
|
|
/// <summary>
|
|
/// The database name.
|
|
/// </summary>
|
|
public string DatabaseName { get; set; }
|
|
|
|
/// <summary>
|
|
/// The MongoDbContext
|
|
/// </summary>
|
|
protected IMongoDbContext MongoDbContext = null;
|
|
|
|
/// <summary>
|
|
/// The constructor taking a connection string and a database name.
|
|
/// </summary>
|
|
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
|
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
|
protected ReadOnlyMongoRepository(string connectionString, string databaseName)
|
|
{
|
|
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
|
/// </summary>
|
|
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
|
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
|
{
|
|
MongoDbContext = mongoDbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
|
/// </summary>
|
|
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
|
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
|
{
|
|
MongoDbContext = new MongoDbContext(mongoDatabase);
|
|
}
|
|
|
|
#region Read
|
|
|
|
/// <summary>
|
|
/// Asynchronously returns one document given its id.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="id">The Id of the document you want to get.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public async Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns one document given its id.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="id">The Id of the document you want to get.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously returns one document given an expression filter.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns one document given an expression filter.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a collection cursor.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if any of the document of the collection matches the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
var count = await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
|
return (count > 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if any of the document of the collection matches the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
var count = HandlePartitioned<TDocument>(partitionKey).Count(filter);
|
|
return (count > 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously returns a list of the documents matching the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a list of the documents matching the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partition key.</param>
|
|
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously counts how many documents match the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partitionKey</param>
|
|
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts how many documents match the filter condition.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
/// <param name="filter">A LINQ expression filter.</param>
|
|
/// <param name="partitionKey">An optional partitionKey</param>
|
|
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
|
{
|
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter).Count();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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 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 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 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 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 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 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).CountAsync(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 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).Count(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 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 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 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).CountAsync(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 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).Count();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Utility Methods
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument with the matching partition key.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <param name="partitionKey">The partion key.</param>
|
|
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
|
protected IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
|
{
|
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> GetCollection<TDocument>() where TDocument : IDocument
|
|
{
|
|
return MongoDbContext.GetCollection<TDocument>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <param name="document">The document.</param>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
|
|
{
|
|
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="document">The document.</param>
|
|
/// <returns></returns>
|
|
protected 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 a potentially partitioned document type.
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <param name="partitionKey">The collection partition key.</param>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey) where TDocument : IDocument
|
|
{
|
|
if (!string.IsNullOrEmpty(partitionKey))
|
|
{
|
|
return GetCollection<TDocument>(partitionKey);
|
|
}
|
|
return GetCollection<TDocument>();
|
|
}
|
|
|
|
/// <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 IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey)
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
return MongoDbContext.GetCollection<TDocument, TKey>(partitionKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a collections for the type TDocument
|
|
/// </summary>
|
|
/// <typeparam name="TDocument">The document type.</typeparam>
|
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
|
/// <returns></returns>
|
|
protected IMongoCollection<TDocument> GetCollection<TDocument, TKey>()
|
|
where TDocument : IDocument<TKey>
|
|
where TKey : IEquatable<TKey>
|
|
{
|
|
return MongoDbContext.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 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>();
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|