Added interface
This commit is contained in:
@@ -2,13 +2,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Bson;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
public abstract class BaseMongoRepository
|
||||
public interface IBaseMongoRepository
|
||||
{
|
||||
#region Read
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
Task<TDocument> GetById<TDocument>(Guid id) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument;
|
||||
|
||||
#endregion Get
|
||||
|
||||
#region Create
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
void AddOne<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public abstract class BaseMongoRepository : IBaseMongoRepository
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public string DatabaseName { get; set; }
|
||||
@@ -25,130 +137,124 @@ namespace MongoDbGenericRepository
|
||||
|
||||
protected IMongoDbContext _mongoDbContext = null;
|
||||
|
||||
#region Get
|
||||
#region Read
|
||||
|
||||
/// <summary>
|
||||
/// A generic GetOne method
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<TDocument> GetOne<TDocument>(string id) where TDocument : IDocument
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
public async Task<TDocument> GetById<TDocument>(Guid id) where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return await GetOne<TDocument>(filter);
|
||||
return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic GetOne method
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<TDocument> GetOne<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic get many method with filter
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <returns></returns>
|
||||
public async Task<List<TDocument>> GetAll<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var count = await GetCollection<TDocument>().CountAsync(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var count = GetCollection<TDocument>().Count(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FindCursor
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns>A cursor for the query</returns>
|
||||
public IFindFluent<TDocument, TDocument> FindCursor<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
var cursor = collection.Find(filter);
|
||||
return cursor;
|
||||
return GetCollection<TDocument>().Find(filter).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic get all method
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <returns></returns>
|
||||
public async Task<List<TDocument>> GetAll<TDocument>() where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
return await collection.Find(new BsonDocument()).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic Exists method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Exists<TDocument>(string id) where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
var query = new BsonDocument("Id", id);
|
||||
var cursor = collection.Find(query);
|
||||
var count = await cursor.CountAsync();
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic count method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> Count<TDocument>(string id) where TDocument : IDocument
|
||||
{
|
||||
var filter = new FilterDefinitionBuilder<TDocument>().Eq("Id", id);
|
||||
return await Count<TDocument>(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic count method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> Count<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
var cursor = collection.Find(filter);
|
||||
var count = await cursor.CountAsync();
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic count method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
var cursor = collection.Find(filter);
|
||||
var count = await cursor.CountAsync();
|
||||
return count;
|
||||
return await GetCollection<TDocument>().CountAsync(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic count method
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter).Count();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
|
||||
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
||||
public async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of projected objects
|
||||
/// </summary>
|
||||
@@ -167,58 +273,73 @@ namespace MongoDbGenericRepository
|
||||
#endregion Get
|
||||
|
||||
#region Create
|
||||
/// <summary>
|
||||
/// A generic Add One method async
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public async Task AddOneAsync<TDocument>(TDocument item) where TDocument : IDocument
|
||||
{
|
||||
await GetCollection<TDocument>().InsertOneAsync(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic method to add a document
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public void AddOne<TDocument>(TDocument item) where TDocument : IDocument
|
||||
|
||||
private void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
if (item.Id == default(Guid))
|
||||
if (document.Id == default(Guid))
|
||||
{
|
||||
item.Id = Guid.NewGuid();
|
||||
document.Id = Guid.NewGuid();
|
||||
}
|
||||
|
||||
if (item.AddedAtUtc == default(DateTime))
|
||||
if (document.AddedAtUtc == default(DateTime))
|
||||
{
|
||||
item.AddedAtUtc = DateTime.UtcNow;
|
||||
document.AddedAtUtc = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
GetCollection<TDocument>().InsertOne(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic Add Many method, performs a bulk insert in mongo
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> items) where TDocument : IDocument
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
await GetCollection<TDocument>().InsertManyAsync(items);
|
||||
FormatDocument(document);
|
||||
await GetCollection<TDocument>().InsertOneAsync(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic Add Many method, performs a bulk insert in mongo
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
public void AddMany<TDocument>(IEnumerable<TDocument> items) where TDocument : IDocument
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public void AddOne<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
GetCollection<TDocument>().InsertMany(items);
|
||||
FormatDocument(document);
|
||||
GetCollection<TDocument>().InsertOne(document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
await GetCollection<TDocument>().InsertManyAsync(documents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
foreach (var document in documents)
|
||||
{
|
||||
FormatDocument(document);
|
||||
}
|
||||
GetCollection<TDocument>().InsertMany(documents);
|
||||
}
|
||||
|
||||
#endregion Create
|
||||
@@ -275,11 +396,57 @@ namespace MongoDbGenericRepository
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var deleteRes = await GetCollection<TDocument>().DeleteManyAsync(filter);
|
||||
return deleteRes.DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic delete many method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||
var deleteRes = await GetCollection<TDocument>().DeleteManyAsync(x => idsTodelete.Contains(x.Id));
|
||||
return deleteRes.DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic delete many method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
|
||||
{
|
||||
if (!documents.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var idsTodelete = documents.Select(e => e.Id).ToArray();
|
||||
var deleteRes = GetCollection<TDocument>().DeleteMany(x => idsTodelete.Contains(x.Id));
|
||||
return deleteRes.DeletedCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic delete many method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
|
||||
{
|
||||
var deleteRes = GetCollection<TDocument>().DeleteMany(filter);
|
||||
return deleteRes.DeletedCount;
|
||||
}
|
||||
#endregion Delete
|
||||
|
||||
#region Update
|
||||
@@ -341,7 +508,6 @@ namespace MongoDbGenericRepository
|
||||
|
||||
#endregion Find And Update
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The private GetCollection method
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user