diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj
index 9567113..7f576ad 100644
--- a/IntegrationTests/IntegrationTests.csproj
+++ b/IntegrationTests/IntegrationTests.csproj
@@ -64,6 +64,7 @@
+
diff --git a/IntegrationTests/ReadPartitionedTests.cs b/IntegrationTests/ReadPartitionedTests.cs
index e76609b..cbf662a 100644
--- a/IntegrationTests/ReadPartitionedTests.cs
+++ b/IntegrationTests/ReadPartitionedTests.cs
@@ -18,7 +18,7 @@ namespace IntegrationTests
public class ReadPartitionedTests : BaseMongoDbRepositoryTests
{
[Test]
- public async Task PartitionedGetById()
+ public async Task PartitionedGetByIdAsync()
{
// Arrange
var document = CreateTestDocument();
@@ -29,6 +29,18 @@ namespace IntegrationTests
Assert.IsNotNull(result);
}
+ [Test]
+ public void PartitionedGetById()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ // Act
+ var result = SUT.GetById(document.Id, PartitionKey);
+ // Assert
+ Assert.IsNotNull(result);
+ }
+
[Test]
public async Task PartitionedGetOneAsync()
{
diff --git a/IntegrationTests/ReadTests.cs b/IntegrationTests/ReadTests.cs
index ccdf48c..e6c186a 100644
--- a/IntegrationTests/ReadTests.cs
+++ b/IntegrationTests/ReadTests.cs
@@ -19,7 +19,7 @@ namespace IntegrationTests
public class ReadTests : BaseMongoDbRepositoryTests
{
[Test]
- public async Task GetById()
+ public async Task GetByIdAsync()
{
// Arrange
var document = CreateTestDocument();
@@ -30,6 +30,18 @@ namespace IntegrationTests
Assert.IsNotNull(result);
}
+ [Test]
+ public void GetById()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ // Act
+ var result = SUT.GetById(document.Id);
+ // Assert
+ Assert.IsNotNull(result);
+ }
+
[Test]
public async Task GetOneAsync()
{
diff --git a/IntegrationTests/UpdatePartitionedTests.cs b/IntegrationTests/UpdatePartitionedTests.cs
new file mode 100644
index 0000000..49776f3
--- /dev/null
+++ b/IntegrationTests/UpdatePartitionedTests.cs
@@ -0,0 +1,51 @@
+using IntegrationTests.Infrastructure;
+using MongoDbGenericRepository.Models;
+using NUnit.Framework;
+using System.Threading.Tasks;
+
+namespace IntegrationTests
+{
+ public class UpdateTestsPartitionedDocument : PartitionedDocument
+ {
+ public UpdateTestsPartitionedDocument() : base("TestPartitionKey")
+ {
+ Version = 2;
+ }
+ public string SomeContent { get; set; }
+ }
+
+ public class UpdatePartitionedTests : BaseMongoDbRepositoryTests
+ {
+ [Test]
+ public void UpdateOne()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ document.SomeContent = "UpdateOneContent";
+ // Act
+ var result = SUT.UpdateOne(document);
+ // Assert
+ Assert.IsTrue(result);
+ var updatedDocument = SUT.GetById(document.Id, PartitionKey);
+ Assert.IsNotNull(updatedDocument);
+ Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
+ }
+
+ [Test]
+ public async Task UpdateOneAsync()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ document.SomeContent = "UpdateOneAsyncContent";
+ // Act
+ var result = await SUT.UpdateOneAsync(document);
+ // Assert
+ Assert.IsTrue(result);
+ var updatedDocument = SUT.GetById(document.Id, PartitionKey);
+ Assert.IsNotNull(updatedDocument);
+ Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
+ }
+ }
+}
diff --git a/IntegrationTests/UpdateTests.cs b/IntegrationTests/UpdateTests.cs
index 5aaf4cd..328c3f3 100644
--- a/IntegrationTests/UpdateTests.cs
+++ b/IntegrationTests/UpdateTests.cs
@@ -1,6 +1,51 @@
-namespace IntegrationTests
+using IntegrationTests.Infrastructure;
+using MongoDbGenericRepository.Models;
+using NUnit.Framework;
+using System.Threading.Tasks;
+
+namespace IntegrationTests
{
- class UpdateTests
+ public class UpdateTestsDocument : Document
{
+ public UpdateTestsDocument()
+ {
+ Version = 2;
+ }
+ public string SomeContent { get; set; }
+ }
+
+ public class UpdateTests : BaseMongoDbRepositoryTests
+ {
+ [Test]
+ public void UpdateOne()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ document.SomeContent = "UpdateOneContent";
+ // Act
+ var result = SUT.UpdateOne(document);
+ // Assert
+ Assert.IsTrue(result);
+ var updatedDocument = SUT.GetById(document.Id);
+ Assert.IsNotNull(updatedDocument);
+ Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
+ }
+
+ [Test]
+ public async Task UpdateOneAsync()
+ {
+ // Arrange
+ var document = CreateTestDocument();
+ SUT.AddOne(document);
+ document.SomeContent = "UpdateOneAsyncContent";
+ // Act
+ var result = await SUT.UpdateOneAsync(document);
+ // Assert
+ Assert.IsTrue(result);
+ var updatedDocument = SUT.GetById(document.Id);
+ Assert.IsNotNull(updatedDocument);
+ Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
+ }
}
}
diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs
index 75b9bfd..a002cb1 100644
--- a/MongoDbGenericRepository/MongoDbContext.cs
+++ b/MongoDbGenericRepository/MongoDbContext.cs
@@ -8,6 +8,9 @@ namespace MongoDbGenericRepository
///
public class MongoDbContext : IMongoDbContext
{
+ private readonly IMongoClient _client;
+ private readonly IMongoDatabase _database;
+
static MongoDbContext()
{
// Avoid legacy UUID representation: use Binary 0x04 subtype.
@@ -16,14 +19,10 @@ namespace MongoDbGenericRepository
public MongoDbContext(string connectionString, string databaseName)
{
-
_client = new MongoClient(connectionString);
_database = _client.GetDatabase(databaseName);
}
- private readonly IMongoClient _client;
- private readonly IMongoDatabase _database;
-
///
/// The private GetCollection method
///
@@ -62,6 +61,11 @@ namespace MongoDbGenericRepository
_database.DropCollection(partitionKey + "-" + Pluralize());
}
+ ///
+ /// Very naively pluralizes a TDocument type name.
+ ///
+ ///
+ ///
private string Pluralize()
{
return typeof(TDocument).Name.ToLower() + "s";
diff --git a/MongoDbGenericRepository/MongoDbRepository.cs b/MongoDbGenericRepository/MongoDbRepository.cs
index 64bae7e..16ffdaf 100644
--- a/MongoDbGenericRepository/MongoDbRepository.cs
+++ b/MongoDbGenericRepository/MongoDbRepository.cs
@@ -55,6 +55,14 @@ namespace MongoDbGenericRepository
/// An optional partition key.
Task GetByIdAsync(Guid id, string partitionKey = null) where TDocument : IDocument;
+ ///
+ /// Returns one document given its id.
+ ///
+ ///
+ /// The Id of the document you want to get.
+ /// An optional partition key.
+ TDocument GetById(Guid id, string partitionKey = null) where TDocument : IDocument;
+
///
/// Asynchronously returns one document given an expression filter.
///
@@ -127,7 +135,27 @@ namespace MongoDbGenericRepository
/// An optional partition key.
long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument;
- #endregion Get
+ #endregion
+
+ #region Update
+
+ ///
+ /// Asynchronously Updates a document.
+ ///
+ ///
+ /// The document with the modifications you want to persist.
+ Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument;
+
+ ///
+ /// Updates a document.
+ ///
+ ///
+ /// The document with the modifications you want to persist.
+ bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument;
+
+ #endregion
+
+
}
@@ -232,6 +260,18 @@ namespace MongoDbGenericRepository
return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync();
}
+ ///
+ /// Returns one document given its id.
+ ///
+ ///
+ /// The Id of the document you want to get.
+ /// An optional partition key.
+ public TDocument GetById(Guid id, string partitionKey = null) where TDocument : IDocument
+ {
+ var filter = Builders.Filter.Eq("Id", id);
+ return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault();
+ }
+
///
/// Asynchronously returns one document given an expression filter.
///
@@ -472,43 +512,27 @@ namespace MongoDbGenericRepository
#region Update
///
- /// Updates a document
+ /// Asynchronously Updates a document.
///
///
- ///
- ///
- public async Task UpdateOneAsync(TDocument entity) where TDocument : IDocument
+ /// The document with the modifications you want to persist.
+ public async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument
{
- var updateRes = await GetCollection().ReplaceOneAsync(x => x.Id == entity.Id, entity);
- return updateRes.ModifiedCount < 1;
+ var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
+ return updateRes.ModifiedCount == 1;
}
///
- /// UpdateOne with filter
+ /// Updates a document.
///
///
- ///
- ///
- ///
- private async Task UpdateOne(FilterDefinition filter, UpdateDefinition update) where TDocument : IDocument
+ /// The document with the modifications you want to persist.
+ public bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument
{
- var updateRes = await GetCollection().UpdateOneAsync(filter, update);
- return updateRes.ModifiedCount;
+ var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
+ return updateRes.ModifiedCount == 1;
}
- ///
- /// UpdateMany with filter
- ///
- ///
- ///
- ///
- ///
- public async Task UpdateMany(FilterDefinition filter, UpdateDefinition update) where TDocument : IDocument
- {
- var collection = GetCollection();
- var updateRes = await collection.UpdateManyAsync(filter, update);
- return updateRes.ModifiedCount;
- }
#endregion Update
#region Find And Update