Continues refactoring tests, added overload for UpdateOne and UpdateOneAsync so that we can pass a LINQ query to the method.

This commit is contained in:
alexandre-spieser
2018-02-10 16:38:47 +00:00
parent bf2119432e
commit 5270271008
16 changed files with 1193 additions and 424 deletions
@@ -0,0 +1,27 @@
using IntegrationTests.Infrastructure;
using MongoDB.Bson.Serialization.Attributes;
using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Models;
using System;
namespace IntegrationTests
{
[CollectionName("TestingCollectionNameAttributePartitionedTKey")]
public class PartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
{
public PartitionedCollectionNameDoc()
{
PartitionKey = "TestPartitionKey";
}
public string PartitionKey { get; set; }
}
public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase<PartitionedCollectionNameDoc>
{
public override string GetClassName()
{
return "CRUDPartitionedCollectionNameAttributeTests";
}
}
}
+23
View File
@@ -0,0 +1,23 @@
using IntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
namespace IntegrationTests
{
public class PartitionedDoc : TestDoc, IPartitionedDocument
{
public PartitionedDoc()
{
PartitionKey = "TestPartitionKey";
}
public string PartitionKey { get; set; }
}
public class CRUDPartitionedTests : MongoDbDocumentTestBase<PartitionedDoc>
{
public override string GetClassName()
{
return "CRUDPartitionedCollectionNameAttributeTests";
}
}
}
@@ -1,5 +1,4 @@
using IntegrationTests.Infrastructure; using IntegrationTests.Infrastructure;
using MongoDB.Bson.Serialization.Attributes;
using MongoDbGenericRepository.Attributes; using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System; using System;
@@ -17,11 +16,11 @@ namespace IntegrationTests
public string PartitionKey { get; set; } public string PartitionKey { get; set; }
} }
public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDBTestBase<TKeyPartitionedCollectionNameDoc, Guid> public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase<TKeyPartitionedCollectionNameDoc, Guid>
{ {
public override string GetClassName() public override string GetClassName()
{ {
return "TKeyPartitionedCollectionNameAttributeTests"; return "CRUDTKeyPartitionedCollectionNameAttributeTests";
} }
} }
} }
+1 -1
View File
@@ -16,7 +16,7 @@ namespace IntegrationTests
} }
[TestFixture] [TestFixture]
public class CRUDTKeyPartitionedTests : MongoDBTestBase<PartitionedTKeyTestDocument, Guid> public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase<PartitionedTKeyTestDocument, Guid>
{ {
public override string GetClassName() public override string GetClassName()
{ {
+1 -1
View File
@@ -11,7 +11,7 @@ namespace IntegrationTests
} }
[TestFixture] [TestFixture]
public class CRUDTKeyTests : MongoDBTestBase<TKeyTestDocument, Guid> public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase<TKeyTestDocument, Guid>
{ {
public override string GetClassName() public override string GetClassName()
{ {
+18
View File
@@ -0,0 +1,18 @@
using IntegrationTests.Infrastructure;
using NUnit.Framework;
namespace IntegrationTests
{
public class TestDocument : TestDoc
{
}
[TestFixture]
public class CRUDTests : MongoDbDocumentTestBase<TestDocument>
{
public override string GetClassName()
{
return "CRUDTests";
}
}
}
@@ -0,0 +1,755 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace IntegrationTests.Infrastructure
{
public class TestDoc : Document
{
public TestDoc()
{
Version = 2;
Nested = new Nested
{
SomeDate = DateTime.UtcNow
};
Children = new List<Child>();
}
public string SomeContent { get; set; }
public Nested Nested { get; set; }
public List<Child> Children { get; set; }
}
[TestFixture]
public abstract class MongoDbDocumentTestBase<T>
where T: TestDoc, new()
{
public T CreateTestDocument()
{
return new T();
}
public abstract string GetClassName();
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
{
var docs = new List<T>();
for (var i = 0; i < numberOfDocumentsToCreate; i++)
{
docs.Add(new T());
}
return docs;
}
/// <summary>
/// The partition key for the collection, if any
/// </summary>
protected string PartitionKey { get; set; }
/// <summary>
/// the name of the test class
/// </summary>
protected string TestClassName { get; set; }
/// <summary>
/// The name of the document used for tests
/// </summary>
protected string DocumentTypeName { get; set; }
/// <summary>
/// SUT: System Under Test
/// </summary>
protected static ITestRepository SUT { get; set; }
public MongoDbDocumentTestBase()
{
var type = CreateTestDocument();
DocumentTypeName = type.GetType().FullName;
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
TestClassName = GetClassName();
}
[OneTimeSetUp]
public void Init()
{
var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString;
SUT = new TestRepository(connectionString, "MongoDbTests");
}
[OneTimeTearDown]
public void Cleanup()
{
// We drop the collection at the end of each test session.
if (!string.IsNullOrEmpty(PartitionKey))
{
SUT.DropTestCollection<T>(PartitionKey);
}
else
{
SUT.DropTestCollection<T>();
}
}
#region Add
[Test]
public void AddOne()
{
// Arrange
var document = new T();
// Act
SUT.AddOne<T>(document);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
Assert.AreEqual(1, count, GetTestName());
}
[Test]
public async Task AddOneAsync()
{
// Arrange
var document = new T();
// Act
await SUT.AddOneAsync<T>(document);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
Assert.AreEqual(1, count, GetTestName());
}
[Test]
public void AddMany()
{
// Arrange
var documents = new List<T> { new T(), new T() };
// Act
SUT.AddMany<T>(documents);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id))
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.AreEqual(2, count, GetTestName());
}
[Test]
public async Task AddManyAsync()
{
// Arrange
var documents = new List<T> { new T(), new T() };
// Act
await SUT.AddManyAsync<T>(documents);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id))
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.AreEqual(2, count, GetTestName());
}
#endregion Add
#region Read
[Test]
public async Task GetByIdAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.GetByIdAsync<T>(document.Id, PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
}
[Test]
public void GetById()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.GetById<T>(document.Id, PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
}
[Test]
public async Task GetOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.GetOneAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
}
[Test]
public void GetOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.GetOne<T>(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
}
[Test]
public void GetCursor()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
var count = cursor.Count();
// Assert
Assert.AreEqual(1, count, GetTestName());
}
[Test]
public async Task AnyAsyncReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.AreEqual(true, result, GetTestName());
}
[Test]
public async Task AnyAsyncReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
// Assert
Assert.AreEqual(false, result, GetTestName());
}
[Test]
public void AnyReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.Any<T>(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.AreEqual(true, result, GetTestName());
}
[Test]
public void AnyReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.Any<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
// Assert
Assert.AreEqual(false, result, GetTestName());
}
[Test]
public async Task GetAllAsync()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany<T>(documents);
// Act
var result = await SUT.GetAllAsync<T>(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.AreEqual(5, result.Count, GetTestName());
}
[Test]
public void GetAll()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany<T>(documents);
// Act
var result = SUT.GetAll<T>(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.AreEqual(5, result.Count, GetTestName());
}
[Test]
public async Task CountAsync()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany<T>(documents);
// Act
var result = await SUT.CountAsync<T>(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.AreEqual(5, result, GetTestName());
}
[Test]
public void Count()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany<T>(documents);
// Act
var result = SUT.Count<T>(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.AreEqual(5, result, GetTestName());
}
#endregion Read
#region Update
[Test]
public void UpdateOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = SUT.UpdateOne<T>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = await SUT.UpdateOneAsync<T>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public void UpdateOneField()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne<T, string>(document, x => x.SomeContent, content);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneFieldAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync<T, string>(document, x => x.SomeContent, content);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public void UpdateOneFieldWithFilter()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneFieldWithFilterAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneAsyncWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = await SUT.UpdateOneAsync<T>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
}
[Test]
public void UpdateOneWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = SUT.UpdateOne<T>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
}
#endregion Update
#region Delete
[Test]
public void DeleteOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.DeleteOne<T>(document);
// Assert
Assert.AreEqual(1, result);
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Test]
public void DeleteOneLinq()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.AreEqual(1, result);
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Test]
public async Task DeleteOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.DeleteOneAsync<T>(document);
// Assert
Assert.AreEqual(1, result);
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Test]
public async Task DeleteOneAsyncLinq()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T>(document);
// Act
var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.AreEqual(1, result);
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Test]
public async Task DeleteManyAsyncLinq()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany<T>(documents);
// Act
var result = await SUT.DeleteManyAsync<T>(e => e.SomeContent == criteria, PartitionKey);
// Assert
Assert.AreEqual(5, result);
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
[Test]
public async Task DeleteManyAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany<T>(documents);
// Act
var result = await SUT.DeleteManyAsync<T>(documents);
// Assert
Assert.AreEqual(5, result);
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
[Test]
public void DeleteManyLinq()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany<T>(documents);
// Act
var result = SUT.DeleteMany<T>(e => e.SomeContent == criteria, PartitionKey);
// Assert
Assert.AreEqual(5, result);
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
[Test]
public void DeleteMany()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany<T>(documents);
// Act
var result = SUT.DeleteMany<T>(documents);
// Assert
Assert.AreEqual(5, result);
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
#endregion Delete
#region Project
[Test]
public async Task ProjectOneAsync()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var document = CreateTestDocument();
document.SomeContent = someContent;
document.Nested.SomeDate = someDate;
SUT.AddOne<T>(document);
// Act
var result = await SUT.ProjectOneAsync<T, MyTestProjection>(
x => x.Id.Equals(document.Id),
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
}
[Test]
public void ProjectOne()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var document = CreateTestDocument();
document.SomeContent = someContent;
document.Nested.SomeDate = someDate;
SUT.AddOne<T>(document);
// Act
var result = SUT.ProjectOne<T, MyTestProjection>(
x => x.Id.Equals(document.Id),
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.IsNotNull(result, GetTestName());
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
}
[Test]
public async Task ProjectManyAsync()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var documents = CreateTestDocuments(5);
documents.ForEach(e =>
{
e.SomeContent = someContent;
e.Nested.SomeDate = someDate;
});
SUT.AddMany<T>(documents);
// Act
var result = await SUT.ProjectManyAsync<T, MyTestProjection>(
x => x.SomeContent == someContent,
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.AreEqual(5, result.Count, GetTestName());
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
}
[Test]
public void ProjectMany()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var documents = CreateTestDocuments(5);
documents.ForEach(e =>
{
e.SomeContent = someContent;
e.Nested.SomeDate = someDate;
});
SUT.AddMany<T>(documents);
// Act
var result = SUT.ProjectMany<T, MyTestProjection>(
x => x.SomeContent == someContent,
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.AreEqual(5, result.Count, GetTestName());
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
}
#endregion Project
#region Test Utils
[MethodImpl(MethodImplOptions.NoInlining)]
private string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private string GetParentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(2);
var method = sf.GetMethod().DeclaringType.Name;
return method;
}
private string GetTestName()
{
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
}
private string GetContent()
{
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
}
#endregion Test Utils
}
}
@@ -26,12 +26,15 @@ namespace IntegrationTests.Infrastructure
{ {
SomeDate = DateTime.UtcNow SomeDate = DateTime.UtcNow
}; };
Children = new List<Child>();
} }
public string SomeContent { get; set; } public string SomeContent { get; set; }
public Nested Nested { get; set; } public Nested Nested { get; set; }
public List<Child> Children { get; set; }
public TId Init<TId>() public TId Init<TId>()
{ {
var idTypeName = typeof(TKey).Name; var idTypeName = typeof(TKey).Name;
@@ -60,7 +63,7 @@ namespace IntegrationTests.Infrastructure
[TestFixture] [TestFixture]
public abstract class MongoDBTestBase<T, TKey> public abstract class MongoDbTKeyDocumentTestBase<T, TKey>
where T: TestDoc<TKey>, new() where T: TestDoc<TKey>, new()
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -101,7 +104,7 @@ namespace IntegrationTests.Infrastructure
/// </summary> /// </summary>
protected static ITestRepository SUT { get; set; } protected static ITestRepository SUT { get; set; }
public MongoDBTestBase() public MongoDbTKeyDocumentTestBase()
{ {
var type = CreateTestDocument(); var type = CreateTestDocument();
DocumentTypeName = type.GetType().FullName; DocumentTypeName = type.GetType().FullName;
@@ -220,9 +223,8 @@ namespace IntegrationTests.Infrastructure
Assert.IsNotNull(result, GetTestName()); Assert.IsNotNull(result, GetTestName());
} }
[Test] [Test]
public async Task PartitionedGetOneAsync() public async Task GetOneAsync()
{ {
// Arrange // Arrange
var document = CreateTestDocument(); var document = CreateTestDocument();
@@ -359,11 +361,165 @@ namespace IntegrationTests.Infrastructure
// Act // Act
var result = SUT.Count<T, TKey>(x => x.SomeContent == content, PartitionKey); var result = SUT.Count<T, TKey>(x => x.SomeContent == content, PartitionKey);
// Assert // Assert
Assert.AreEqual(5, result); Assert.AreEqual(5, result, GetTestName());
} }
#endregion Read #endregion Read
#region Update
[Test]
public void UpdateOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = SUT.UpdateOne<T, TKey>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = await SUT.UpdateOneAsync<T, TKey>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public void UpdateOneField()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne<T, TKey, string>(document, x => x.SomeContent, content);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneFieldAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync<T, TKey, string>(document, x => x.SomeContent, content);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public void UpdateOneFieldWithFilter()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneFieldWithFilterAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.IsTrue(result, GetTestName());
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument, GetTestName());
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
}
[Test]
public async Task UpdateOneAsyncWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = await SUT.UpdateOneAsync<T, TKey>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
}
[Test]
public void UpdateOneWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<T, TKey>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = SUT.UpdateOne<T, TKey>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
}
#endregion Update
#region Delete #region Delete
[Test] [Test]
+6 -4
View File
@@ -48,8 +48,11 @@
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DeleteTests.cs" /> <Compile Include="CRUDPartitionedTests.cs" />
<Compile Include="CRUDTKeyPartitionedCollectionNameAttributeTests.cs" /> <Compile Include="CRUDTKeyPartitionedCollectionNameAttributeTests.cs" />
<Compile Include="CRUDTests.cs" />
<Compile Include="DeleteTests.cs" />
<Compile Include="CRUDPartitionedCollectionNameAttributeTests.cs" />
<Compile Include="CRUDTKeyPartitionedTests.cs" /> <Compile Include="CRUDTKeyPartitionedTests.cs" />
<Compile Include="CRUDTKeyTests.cs" /> <Compile Include="CRUDTKeyTests.cs" />
<Compile Include="DeletePartitionedTests.cs" /> <Compile Include="DeletePartitionedTests.cs" />
@@ -59,7 +62,8 @@
<Compile Include="Infrastructure\Child.cs" /> <Compile Include="Infrastructure\Child.cs" />
<Compile Include="Infrastructure\GlobalVariables.cs" /> <Compile Include="Infrastructure\GlobalVariables.cs" />
<Compile Include="Infrastructure\ITestRepository.cs" /> <Compile Include="Infrastructure\ITestRepository.cs" />
<Compile Include="Infrastructure\MongoDBTestBase.cs" /> <Compile Include="Infrastructure\MongoDbDocumentTestBase.cs" />
<Compile Include="Infrastructure\MongoDbTKeyDocumentTestBase.cs" />
<Compile Include="Infrastructure\RandomExtensions.cs" /> <Compile Include="Infrastructure\RandomExtensions.cs" />
<Compile Include="Infrastructure\TestRepository.cs" /> <Compile Include="Infrastructure\TestRepository.cs" />
<Compile Include="CreateTests.cs" /> <Compile Include="CreateTests.cs" />
@@ -68,9 +72,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadPartitionedTests.cs" /> <Compile Include="ReadPartitionedTests.cs" />
<Compile Include="ReadTests.cs" /> <Compile Include="ReadTests.cs" />
<Compile Include="UpdatePartitionedTKeyTests.cs" />
<Compile Include="UpdatePartitionedTests.cs" /> <Compile Include="UpdatePartitionedTests.cs" />
<Compile Include="UpdateTKeyTests.cs" />
<Compile Include="UpdateTests.cs" /> <Compile Include="UpdateTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -1,117 +0,0 @@
using IntegrationTests.Infrastructure;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace IntegrationTests
{
public class UpdateTestsPartitionedTKeyDocument : IDocument<Guid>, IPartitionedDocument
{
[BsonId]
public Guid Id { get; set; }
public int Version { get; set; }
public UpdateTestsPartitionedTKeyDocument()
{
Id = Guid.NewGuid();
Version = 2;
PartitionKey = "TestPartitionKey";
Children = new List<Child>();
}
public string PartitionKey { get; set; }
public string SomeContent { get; set; }
public List<Child> Children { get; set; }
}
[TestFixture]
public class UpdatePartitionedTKeyTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedTKeyDocument>
{
[Test]
public void PartitionedUpdateOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
document.SomeContent = "UpdateOneContent";
// Act
var result = SUT.UpdateOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
}
[Test]
public async Task PartitionedUpdateOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
document.SomeContent = "UpdateOneAsyncContent";
// Act
var result = await SUT.UpdateOneAsync<UpdateTestsPartitionedTKeyDocument, Guid>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
}
[Test]
public async Task UpdateOneAsyncWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders<UpdateTestsPartitionedTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = await SUT.UpdateOneAsync<UpdateTestsPartitionedTKeyDocument, Guid>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, document.PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
[Test]
public void UpdateOneWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders<UpdateTestsPartitionedTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = SUT.UpdateOne<UpdateTestsPartitionedTKeyDocument, Guid>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, document.PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
}
}
-195
View File
@@ -1,195 +0,0 @@
using IntegrationTests.Infrastructure;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace IntegrationTests
{
public class UpdateTestsTKeyDocument : IDocument<Guid>
{
[BsonId]
public Guid Id { get; set; }
public int Version { get; set; }
public UpdateTestsTKeyDocument()
{
Id = Guid.NewGuid();
Version = 2;
Children = new List<Child>();
}
public string SomeContent { get; set; }
public List<Child> Children { get; set; }
}
[TestFixture]
public class UpdateTKeyTests : BaseMongoDbRepositoryTests<UpdateTestsTKeyDocument>
{
[Test]
public void UpdateOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
document.SomeContent = "UpdateOneContent";
// Act
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
}
[Test]
public async Task UpdateOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
document.SomeContent = "UpdateOneAsyncContent";
// Act
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid>(document);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
}
[Test]
public async Task UpdateOneAsyncWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders<UpdateTestsTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
[Test]
public void UpdateOneWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders<UpdateTestsTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid>(document, updateDef);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
[Test]
public void UpdateOneWithFieldSelector()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
// Act
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid, List<Child>>(document, x => x.Children, childrenToAdd);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
[Test]
public async Task UpdateOneAsyncWithFieldSelector()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
// Act
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid, List<Child>>(document, x => x.Children, childrenToAdd);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
[Test]
public void UpdateOneWithFilterAndFieldSelector()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
var childrenToAdd = new List<Child>
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
// Act
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid, List<Child>>(filter, x => x.Children, childrenToAdd);
// Assert
Assert.IsTrue(result);
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
}
}
}
@@ -159,6 +159,18 @@ namespace MongoDbGenericRepository
Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value) Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
where TDocument : IDocument; where TDocument : IDocument;
/// <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>
bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
where TDocument : IDocument;
/// <summary> /// <summary>
/// Takes a document you want to modify and applies the update you have defined in MongoDb. /// Takes a document you want to modify and applies the update you have defined in MongoDb.
/// </summary> /// </summary>
@@ -168,6 +180,18 @@ namespace MongoDbGenericRepository
Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update) Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
where TDocument : IDocument; where TDocument : IDocument;
/// <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>
Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
where TDocument : IDocument;
/// <summary> /// <summary>
/// Takes a document you want to modify and applies the update you have defined in MongoDb. /// Takes a document you want to modify and applies the update you have defined in MongoDb.
/// </summary> /// </summary>
@@ -223,6 +247,19 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <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>
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>;
/// <summary> /// <summary>
/// Updates the property field with the given value update a property field in entities. /// Updates the property field with the given value update a property field in entities.
/// </summary> /// </summary>
@@ -236,6 +273,19 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <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>
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>;
/// <summary> /// <summary>
/// Updates the property field with the given value update a property field in entities. /// Updates the property field with the given value update a property field in entities.
/// </summary> /// </summary>
@@ -19,18 +19,12 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
IMongoDatabase Database { get; } IMongoDatabase Database { get; }
/// <summary>
/// The private GetCollection method
/// </summary>
/// <typeparam name="TDocument"></typeparam>
IMongoCollection<TDocument> GetCollection<TDocument>();
/// <summary> /// <summary>
/// Returns a collection for a document type that has a partition key. /// Returns a collection for a document type that has a partition key.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument; IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument;
/// <summary> /// <summary>
/// Returns a collection for a document type that has a partition key. /// Returns a collection for a document type that has a partition key.
@@ -38,7 +32,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey) IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
+125 -55
View File
@@ -53,7 +53,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument public virtual async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
{ {
FormatDocument(document); FormatDocument(document);
await HandlePartitioned(document).InsertOneAsync(document); await HandlePartitioned(document).InsertOneAsync(document);
@@ -65,7 +65,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public void AddOne<TDocument>(TDocument document) where TDocument : IDocument public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument
{ {
FormatDocument(document); FormatDocument(document);
HandlePartitioned(document).InsertOne(document); HandlePartitioned(document).InsertOne(document);
@@ -77,7 +77,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{ {
if (!documents.Any()) if (!documents.Any())
{ {
@@ -96,7 +96,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{ {
if (!documents.Any()) if (!documents.Any())
{ {
@@ -120,7 +120,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="document">The document you want to add.</param>
public async Task AddOneAsync<TDocument, TKey>(TDocument document) public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -135,7 +135,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="document">The document you want to add.</param>
public void AddOne<TDocument, TKey>(TDocument document) public virtual void AddOne<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -150,7 +150,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="documents">The documents you want to add.</param>
public async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -172,7 +172,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="documents">The documents you want to add.</param>
public void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -197,7 +197,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
public async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
{ {
var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument); var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
return updateRes.ModifiedCount == 1; return updateRes.ModifiedCount == 1;
@@ -208,7 +208,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
public bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument public virtual bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
{ {
var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument); var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
return updateRes.ModifiedCount == 1; return updateRes.ModifiedCount == 1;
@@ -220,7 +220,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param> /// <param name="update">The update definition for the document.</param>
public async Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update) public virtual async Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
where TDocument : IDocument where TDocument : IDocument
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id); var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
@@ -236,7 +236,7 @@ namespace MongoDbGenericRepository
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
public bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value) public virtual bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
where TDocument : IDocument where TDocument : IDocument
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id); var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
@@ -252,7 +252,7 @@ namespace MongoDbGenericRepository
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
public async Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value) public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
where TDocument : IDocument where TDocument : IDocument
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id); var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
@@ -269,7 +269,7 @@ namespace MongoDbGenericRepository
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null) public virtual bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
{ {
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey); var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
@@ -277,6 +277,23 @@ namespace MongoDbGenericRepository
return updateRes.ModifiedCount == 1; 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>
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;
}
/// <summary> /// <summary>
/// Updates the property field with the given value update a property field in entities. /// Updates the property field with the given value update a property field in entities.
/// </summary> /// </summary>
@@ -286,7 +303,7 @@ namespace MongoDbGenericRepository
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public async Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null) public virtual async Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
{ {
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey); var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument>() : GetCollection<TDocument>(partitionKey);
@@ -294,13 +311,30 @@ namespace MongoDbGenericRepository
return updateRes.ModifiedCount == 1; 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>
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;
}
/// <summary> /// <summary>
/// Takes a document you want to modify and applies the update you have defined in MongoDb. /// Takes a document you want to modify and applies the update you have defined in MongoDb.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param> /// <param name="update">The update definition for the document.</param>
public bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update) public virtual bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
where TDocument : IDocument where TDocument : IDocument
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id); var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
@@ -318,7 +352,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
public async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument) public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -333,7 +367,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
public bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument) public virtual bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -349,7 +383,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param> /// <param name="update">The update definition for the document.</param>
public async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update) public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -365,7 +399,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param> /// <param name="update">The update definition for the document.</param>
public bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update) public virtual bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -383,7 +417,7 @@ namespace MongoDbGenericRepository
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
public async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value) public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -401,7 +435,7 @@ namespace MongoDbGenericRepository
/// <param name="documentToModify">The document you want to modify.</param> /// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
public bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value) public virtual bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -420,7 +454,7 @@ namespace MongoDbGenericRepository
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null) 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 TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -430,7 +464,25 @@ namespace MongoDbGenericRepository
} }
/// <summary> /// <summary>
/// Updates the property field with the given value update a property field in entities. /// 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>
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>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = await collection.UpdateOneAsync(Builders<TDocument>.Filter.Where(filter), Builders<TDocument>.Update.Set(field, value));
return updateRes.ModifiedCount == 1;
}
/// <summary>
/// Updates the property field with the given value.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
@@ -439,7 +491,7 @@ namespace MongoDbGenericRepository
/// <param name="field">The field selector.</param> /// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param> /// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null) 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 TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -448,6 +500,24 @@ namespace MongoDbGenericRepository
return updateRes.ModifiedCount == 1; 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>
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>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = collection.UpdateOne(Builders<TDocument>.Filter.Where(filter), Builders<TDocument>.Update.Set(field, value));
return updateRes.ModifiedCount == 1;
}
#endregion Update #endregion Update
#region Delete #region Delete
@@ -458,7 +528,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param> /// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument
{ {
return (await HandlePartitioned(document).DeleteOneAsync(x => x.Id == document.Id)).DeletedCount; return (await HandlePartitioned(document).DeleteOneAsync(x => x.Id == document.Id)).DeletedCount;
} }
@@ -469,7 +539,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param> /// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument public virtual long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument
{ {
return HandlePartitioned(document).DeleteOne(x => x.Id == document.Id).DeletedCount; return HandlePartitioned(document).DeleteOne(x => x.Id == document.Id).DeletedCount;
} }
@@ -481,7 +551,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
{ {
return HandlePartitioned<TDocument>(partitionKey).DeleteOne(filter).DeletedCount; return HandlePartitioned<TDocument>(partitionKey).DeleteOne(filter).DeletedCount;
} }
@@ -493,7 +563,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
{ {
return (await HandlePartitioned<TDocument>(partitionKey).DeleteOneAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument>(partitionKey).DeleteOneAsync(filter)).DeletedCount;
} }
@@ -505,7 +575,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
{ {
return (await HandlePartitioned<TDocument>(partitionKey).DeleteManyAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
} }
@@ -516,7 +586,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param> /// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{ {
if (!documents.Any()) if (!documents.Any())
{ {
@@ -532,7 +602,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param> /// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{ {
if (!documents.Any()) if (!documents.Any())
{ {
@@ -549,7 +619,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
{ {
return HandlePartitioned<TDocument>(partitionKey).DeleteMany(filter).DeletedCount; return HandlePartitioned<TDocument>(partitionKey).DeleteMany(filter).DeletedCount;
} }
@@ -565,7 +635,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteOne<TDocument, TKey>(TDocument document) public virtual long DeleteOne<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -580,7 +650,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document) public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -596,7 +666,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -611,7 +681,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -626,7 +696,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -640,7 +710,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -659,7 +729,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key for 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> /// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -679,7 +749,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
public long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -698,7 +768,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual async Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
where TProjection : class where TProjection : class
{ {
@@ -716,7 +786,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual async Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
where TProjection : class where TProjection : class
@@ -734,7 +804,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
where TProjection : class where TProjection : class
{ {
@@ -752,7 +822,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
where TProjection : class where TProjection : class
@@ -770,7 +840,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
where TProjection : class where TProjection : class
{ {
@@ -788,7 +858,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
where TProjection : class where TProjection : class
@@ -806,7 +876,7 @@ namespace MongoDbGenericRepository
/// <param name="filter"></param> /// <param name="filter"></param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
where TProjection : class where TProjection : class
{ {
@@ -824,7 +894,7 @@ namespace MongoDbGenericRepository
/// <param name="filter">The document filter.</param> /// <param name="filter">The document filter.</param>
/// <param name="projection">The projection expression.</param> /// <param name="projection">The projection expression.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null) public virtual List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
where TProjection : class where TProjection : class
@@ -848,7 +918,7 @@ namespace MongoDbGenericRepository
/// <param name="groupingCriteria">The grouping criteria.</param> /// <param name="groupingCriteria">The grouping criteria.</param>
/// <param name="groupProjection">The projected group result.</param> /// <param name="groupProjection">The projected group result.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
public List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>( public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
Expression<Func<TDocument, TGroupKey>> groupingCriteria, Expression<Func<TDocument, TGroupKey>> groupingCriteria,
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection, Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
string partitionKey = null) string partitionKey = null)
@@ -873,7 +943,7 @@ namespace MongoDbGenericRepository
/// <param name="selector">The grouping criteria.</param> /// <param name="selector">The grouping criteria.</param>
/// <param name="projection">The projected group result.</param> /// <param name="projection">The projected group result.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
public List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(Expression<Func<TDocument, bool>> filter, public virtual List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, TGroupKey>> selector, Expression<Func<TDocument, TGroupKey>> selector,
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection, Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection,
string partitionKey = null) string partitionKey = null)
@@ -898,7 +968,7 @@ namespace MongoDbGenericRepository
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</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> /// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) public virtual async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null)
where TDocument : IDocument where TDocument : IDocument
{ {
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); return await HandlePartitioned<TDocument>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
@@ -913,7 +983,7 @@ namespace MongoDbGenericRepository
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</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> /// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
public async Task<List<TDocument>> GetPaginatedAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) public virtual async Task<List<TDocument>> GetPaginatedAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -930,7 +1000,7 @@ namespace MongoDbGenericRepository
/// <param name="update"></param> /// <param name="update"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <returns></returns> /// <returns></returns>
public async Task<TDocument> GetAndUpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options) where TDocument : IDocument public virtual async Task<TDocument> GetAndUpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options) where TDocument : IDocument
{ {
return await GetCollection<TDocument>().FindOneAndUpdateAsync(filter, update, options); return await GetCollection<TDocument>().FindOneAndUpdateAsync(filter, update, options);
} }
@@ -944,7 +1014,7 @@ namespace MongoDbGenericRepository
/// <param name="update"></param> /// <param name="update"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <returns></returns> /// <returns></returns>
public async Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options) public virtual async Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
+19 -19
View File
@@ -60,15 +60,15 @@ namespace MongoDbGenericRepository
} }
/// <summary> /// <summary>
/// The private GetCollection method /// Extracts the CollectionName attribute from the entity type, if any.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns></returns> /// <returns>The name of the collection in which the TDocument is stored.</returns>
public IMongoCollection<TDocument> GetCollection<TDocument>() private string GetAttributeCollectionName<TDocument>()
{ {
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; return (typeof(TDocument).GetTypeInfo()
var name = collectionNameAttribute?.Name ?? Pluralize<TDocument>(); .GetCustomAttributes(typeof(CollectionNameAttribute))
return Database.GetCollection<TDocument>(name); .FirstOrDefault() as CollectionNameAttribute)?.Name;
} }
/// <summary> /// <summary>
@@ -76,11 +76,13 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument
{ {
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; if (string.IsNullOrEmpty(partitionKey))
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>(); {
return Database.GetCollection<TDocument>(name); return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
}
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
} }
/// <summary> /// <summary>
@@ -93,9 +95,11 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; if (string.IsNullOrEmpty(partitionKey))
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>(); {
return Database.GetCollection<TDocument>(name); return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
}
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
} }
/// <summary> /// <summary>
@@ -104,9 +108,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>() public void DropCollection<TDocument>()
{ {
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; Database.DropCollection(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
var name = collectionNameAttribute?.Name ?? Pluralize<TDocument>();
Database.DropCollection(name);
} }
/// <summary> /// <summary>
@@ -115,9 +117,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>(string partitionKey) public void DropCollection<TDocument>(string partitionKey)
{ {
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; Database.DropCollection(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>();
Database.DropCollection(name);
} }
/// <summary> /// <summary>
@@ -425,26 +425,13 @@ namespace MongoDbGenericRepository
/// <typeparam name="TKey">The type of the primary key.</typeparam> /// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="partitionKey">The collection partition key.</param> /// <param name="partitionKey">The collection partition key.</param>
/// <returns></returns> /// <returns></returns>
protected IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey) protected IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return MongoDbContext.GetCollection<TDocument, TKey>(partitionKey); 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> /// <summary>
/// Gets a collections for a potentially partitioned document type. /// Gets a collections for a potentially partitioned document type.
/// </summary> /// </summary>