Added interface

This commit is contained in:
alexandre-spieser
2017-08-27 15:37:06 +00:00
parent 63c3b0f642
commit c24fa78907
5 changed files with 339 additions and 151 deletions
+9
View File
@@ -0,0 +1,9 @@
using MongoDbGenericRepository;
namespace IntegrationTests
{
public interface ITestsRepository : IBaseMongoRepository
{
void DropTestCollection<TDocument>();
}
}
+32 -33
View File
@@ -7,54 +7,31 @@ using System.Configuration;
namespace IntegrationTests namespace IntegrationTests
{ {
public class InsertTestsRepository : BaseMongoRepository public class TestsRepository : BaseMongoRepository, ITestsRepository
{ {
/// <inheritdoc /> /// <inheritdoc />
public InsertTestsRepository(string connectionString, string databaseName) : base(connectionString, databaseName) public TestsRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
{ {
} }
public void DropTestCollection<TDocument>() public void DropTestCollection<TDocument>()
{ {
_mongoDbContext _mongoDbContext.DropCollection<TDocument>();
} }
} }
public class InsertTests public class InsertTests
{ {
private class InsertTestsDocument : Document
{
public InsertTestsDocument()
{
Version = 2;
}
public string SomeContent { get; set; }
}
private void Cleanup(InsertTestsDocument document)
{
SUT.DeleteOne(document);
Assert.AreEqual(0, SUT.Count<InsertTestsDocument>(e => e.Id == document.Id));
}
private void Cleanup(List<InsertTestsDocument> documents)
{
SUT.DeleteMany(documents);
SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
}
/// <summary> /// <summary>
/// SUT: System Under Test /// SUT: System Under Test
/// </summary> /// </summary>
private static InsertTestsRepository SUT { get; set; } private static ITestsRepository SUT { get; set; }
[OneTimeSetUp] [OneTimeSetUp]
public void Init() public void Init()
{ {
var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString; var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString;
SUT = new InsertTestsRepository(connectionString, "MongoDbTests"); SUT = new TestsRepository(connectionString, "MongoDbTests");
} }
[Test] [Test]
@@ -67,8 +44,18 @@ namespace IntegrationTests
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id); long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id);
Assert.AreEqual(1, count); Assert.AreEqual(1, count);
// Cleanup }
Cleanup(document);
[Test]
public void InsertOneAsync()
{
// Arrange
var document = new InsertTestsDocument();
// Act
SUT.AddOne(document);
// Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id);
Assert.AreEqual(1, count);
} }
[Test] [Test]
@@ -81,8 +68,20 @@ namespace IntegrationTests
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id); long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.AreEqual(2, count); Assert.AreEqual(2, count);
// Cleanup }
Cleanup(documents);
}
#region Utils
private class InsertTestsDocument : Document
{
public InsertTestsDocument()
{
Version = 2;
}
public string SomeContent { get; set; }
}
#endregion
} }
} }
+2
View File
@@ -56,8 +56,10 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ITestsRepository.cs" />
<Compile Include="InsertTests.cs" /> <Compile Include="InsertTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config"> <None Include="App.config">
+12
View File
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IntegrationTests
{
class UpdateTests
{
}
}
+285 -119
View File
@@ -2,13 +2,125 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Bson;
using System.Linq.Expressions; using System.Linq.Expressions;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System.Linq;
namespace MongoDbGenericRepository 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 ConnectionString { get; set; }
public string DatabaseName { get; set; } public string DatabaseName { get; set; }
@@ -25,130 +137,124 @@ namespace MongoDbGenericRepository
protected IMongoDbContext _mongoDbContext = null; protected IMongoDbContext _mongoDbContext = null;
#region Get #region Read
/// <summary> /// <summary>
/// A generic GetOne method /// Asynchronously returns one document given its id.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="id"></param> /// <param name="id">The Id of the document you want to get.</param>
/// <returns></returns> public async Task<TDocument> GetById<TDocument>(Guid id) where TDocument : IDocument
public async Task<TDocument> GetOne<TDocument>(string id) where TDocument : IDocument
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", id); var filter = Builders<TDocument>.Filter.Eq("Id", id);
return await GetOne<TDocument>(filter); return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync();
} }
/// <summary> /// <summary>
/// A generic GetOne method /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="id"></param> /// <param name="filter">A LINQ expression filter.</param>
/// <returns></returns> public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
public async Task<TDocument> GetOne<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
{ {
return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync(); return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync();
} }
/// <summary> /// <summary>
/// A generic get many method with filter /// Returns one document given an expression filter.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <returns></returns> /// <param name="filter">A LINQ expression filter.</param>
public async Task<List<TDocument>> GetAll<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument 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(); return await GetCollection<TDocument>().Find(filter).ToListAsync();
} }
/// <summary> /// <summary>
/// FindCursor /// Returns a list of the documents matching the filter condition.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter"></param> /// <param name="filter">A LINQ expression filter.</param>
/// <returns>A cursor for the query</returns> public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
public IFindFluent<TDocument, TDocument> FindCursor<TDocument>(FilterDefinition<TDocument> filter) where TDocument : IDocument
{ {
var collection = GetCollection<TDocument>(); return GetCollection<TDocument>().Find(filter).ToList();
var cursor = collection.Find(filter);
return cursor;
} }
/// <summary> /// <summary>
/// A generic get all method /// Asynchronously counts how many documents match the filter condition.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <returns></returns> /// <param name="filter">A LINQ expression filter.</param>
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>
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
{ {
var collection = GetCollection<TDocument>(); return await GetCollection<TDocument>().CountAsync(filter);
var cursor = collection.Find(filter);
var count = await cursor.CountAsync();
return count;
} }
/// <summary> /// <summary>
/// A generic count method /// Counts how many documents match the filter condition.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="id"></param> /// <param name="filter">A LINQ expression filter.</param>
/// <returns></returns>
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument public long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
{ {
return GetCollection<TDocument>().Find(filter).Count(); 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> /// <summary>
/// Returns a list of projected objects /// Returns a list of projected objects
/// </summary> /// </summary>
@@ -167,58 +273,73 @@ namespace MongoDbGenericRepository
#endregion Get #endregion Get
#region Create #region Create
/// <summary>
/// A generic Add One method async
/// </summary> private void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument
/// <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); if (document.Id == default(Guid))
{
document.Id = Guid.NewGuid();
}
if (document.AddedAtUtc == default(DateTime))
{
document.AddedAtUtc = DateTime.UtcNow;
}
} }
/// <summary> /// <summary>
/// A generic method to add a document /// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="item"></param> /// <param name="document">The document you want to add.</param>
/// <returns></returns> public async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
public void AddOne<TDocument>(TDocument item) where TDocument : IDocument
{ {
if (item.Id == default(Guid)) FormatDocument(document);
{ await GetCollection<TDocument>().InsertOneAsync(document);
item.Id = Guid.NewGuid();
}
if (item.AddedAtUtc == default(DateTime))
{
item.AddedAtUtc = DateTime.UtcNow;
}
GetCollection<TDocument>().InsertOne(item);
} }
/// <summary> /// <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> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="items"></param> /// <param name="document">The document you want to add.</param>
/// <returns></returns> public void AddOne<TDocument>(TDocument document) where TDocument : IDocument
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> items) where TDocument : IDocument
{ {
await GetCollection<TDocument>().InsertManyAsync(items); FormatDocument(document);
GetCollection<TDocument>().InsertOne(document);
} }
/// <summary> /// <summary>
/// A generic Add Many method, performs a bulk insert in mongo /// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="items"></param> /// <param name="document">The document you want to add.</param>
/// <returns></returns> public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
public void AddMany<TDocument>(IEnumerable<TDocument> items) where TDocument : IDocument
{ {
GetCollection<TDocument>().InsertMany(items); 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 #endregion Create
@@ -275,11 +396,57 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter"></param> /// <param name="filter"></param>
/// <returns></returns> /// <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); var deleteRes = await GetCollection<TDocument>().DeleteManyAsync(filter);
return deleteRes.DeletedCount; 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 #endregion Delete
#region Update #region Update
@@ -341,7 +508,6 @@ namespace MongoDbGenericRepository
#endregion Find And Update #endregion Find And Update
/// <summary> /// <summary>
/// The private GetCollection method /// The private GetCollection method
/// </summary> /// </summary>