added tests for partitioned collections

This commit is contained in:
alexandre-spieser
2017-08-27 17:35:21 +00:00
parent 5817486f10
commit 5caed5b3a9
13 changed files with 360 additions and 180 deletions
@@ -0,0 +1,68 @@
using IntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace IntegrationTests
{
public class CreateTestsPartitionedDocument : PartitionedDocument
{
public CreateTestsPartitionedDocument() : base("TestPartitionKey")
{
Version = 1;
}
public string SomeContent { get; set; }
}
public class CreatePartitionedTests : BaseMongoDbRepositoryTests<CreateTestsPartitionedDocument>
{
[Test]
public void AddOne()
{
// Arrange
var document = new CreateTestsPartitionedDocument();
// Act
SUT.AddOne(document);
// Assert
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
Assert.AreEqual(1, count);
}
[Test]
public async Task AddOneAsync()
{
// Arrange
var document = new CreateTestsPartitionedDocument();
// Act
await SUT.AddOneAsync(document);
// Assert
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
Assert.AreEqual(1, count);
}
[Test]
public void AddMany()
{
// Arrange
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
// Act
SUT.AddMany(documents);
// Assert
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
Assert.AreEqual(2, count);
}
[Test]
public async Task AddManyAsync()
{
// Arrange
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
// Act
await SUT.AddManyAsync(documents);
// Assert
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
Assert.AreEqual(2, count);
}
}
}
+15 -39
View File
@@ -2,41 +2,31 @@
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace IntegrationTests
{ {
public class CreateTests public class CreateTestsDocument : Document
{ {
/// <summary> public CreateTestsDocument()
/// SUT: System Under Test
/// </summary>
private static ITestRepository SUT { get; set; }
[OneTimeSetUp]
public void Init()
{ {
var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString; Version = 2;
SUT = new TestRepository(connectionString, "MongoDbTests"); }
public string SomeContent { get; set; }
} }
[OneTimeTearDown] [TestFixture]
public void Cleanup() public class CreateTests : BaseMongoDbRepositoryTests<CreateTestsDocument>
{ {
// We drop the collection at the end of each test session.
SUT.DropTestCollection<InsertTestsDocument>();
}
[Test] [Test]
public void AddOne() public void AddOne()
{ {
// Arrange // Arrange
var document = new InsertTestsDocument(); var document = new CreateTestsDocument();
// Act // Act
SUT.AddOne(document); SUT.AddOne(document);
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id); long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
Assert.AreEqual(1, count); Assert.AreEqual(1, count);
} }
@@ -44,11 +34,11 @@ namespace IntegrationTests
public async Task AddOneAsync() public async Task AddOneAsync()
{ {
// Arrange // Arrange
var document = new InsertTestsDocument(); var document = new CreateTestsDocument();
// Act // Act
await SUT.AddOneAsync(document); await SUT.AddOneAsync(document);
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id); long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
Assert.AreEqual(1, count); Assert.AreEqual(1, count);
} }
@@ -56,11 +46,11 @@ namespace IntegrationTests
public void AddMany() public void AddMany()
{ {
// Arrange // Arrange
var documents = new List<InsertTestsDocument> { new InsertTestsDocument(), new InsertTestsDocument() }; var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
// Act // Act
SUT.AddMany(documents); SUT.AddMany(documents);
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id); long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.AreEqual(2, count); Assert.AreEqual(2, count);
} }
@@ -68,26 +58,12 @@ namespace IntegrationTests
public async Task AddManyAsync() public async Task AddManyAsync()
{ {
// Arrange // Arrange
var documents = new List<InsertTestsDocument> { new InsertTestsDocument(), new InsertTestsDocument() }; var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
// Act // Act
await SUT.AddManyAsync(documents); await SUT.AddManyAsync(documents);
// Assert // Assert
long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id); long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.AreEqual(2, count); Assert.AreEqual(2, count);
} }
#region Utils
private class InsertTestsDocument : Document
{
public InsertTestsDocument()
{
Version = 2;
}
public string SomeContent { get; set; }
}
#endregion
} }
} }
@@ -0,0 +1,51 @@
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System.Configuration;
namespace IntegrationTests.Infrastructure
{
public class BaseMongoDbRepositoryTests<T> where T : Document, new()
{
public T GetDocumentInstance()
{
return new T();
}
public BaseMongoDbRepositoryTests()
{
var type = GetDocumentInstance();
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
}
public string PartitionKey { get; set; }
/// <summary>
/// SUT: System Under Test
/// </summary>
protected static ITestRepository SUT { get; set; }
[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>();
}
}
}
}
@@ -5,5 +5,6 @@ namespace IntegrationTests
public interface ITestRepository : IBaseMongoRepository public interface ITestRepository : IBaseMongoRepository
{ {
void DropTestCollection<TDocument>(); void DropTestCollection<TDocument>();
void DropTestCollection<TDocument>(string partitionKey);
} }
} }
@@ -13,5 +13,10 @@ namespace IntegrationTests.Infrastructure
{ {
_mongoDbContext.DropCollection<TDocument>(); _mongoDbContext.DropCollection<TDocument>();
} }
public void DropTestCollection<TDocument>(string partitionKey)
{
_mongoDbContext.DropCollection<TDocument>(partitionKey);
}
} }
} }
+3
View File
@@ -56,10 +56,13 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
<Compile Include="CreatePartitionedTests.cs" />
<Compile Include="Infrastructure\ITestRepository.cs" /> <Compile Include="Infrastructure\ITestRepository.cs" />
<Compile Include="Infrastructure\TestRepository.cs" /> <Compile Include="Infrastructure\TestRepository.cs" />
<Compile Include="CreateTests.cs" /> <Compile Include="CreateTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadTests.cs" />
<Compile Include="UpdateTests.cs" /> <Compile Include="UpdateTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+19
View File
@@ -0,0 +1,19 @@
using MongoDbGenericRepository.Models;
using NUnit.Framework;
namespace IntegrationTests
{
public class ReadTestsDocument : Document
{
public ReadTestsDocument()
{
Version = 2;
}
public string SomeContent { get; set; }
}
[TestFixture]
public class ReadTests
{
}
}
+1 -7
View File
@@ -1,10 +1,4 @@
using System; namespace IntegrationTests
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IntegrationTests
{ {
class UpdateTests class UpdateTests
{ {
+9 -4
View File
@@ -9,20 +9,25 @@ namespace MongoDbGenericRepository
/// The private GetCollection method /// The private GetCollection method
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <returns></returns>
IMongoCollection<TDocument> GetCollection<TDocument>(); IMongoCollection<TDocument> GetCollection<TDocument>();
/// <summary> /// <summary>
/// The private GetCollection method /// Returns a collection for a document type that has a partition key.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <returns></returns> /// <param name="partitionKey">The value of the partition key.</param>
IMongoCollection<TDocument> GetCollection<TDocument>(TDocument document) where TDocument : IDocument; IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument;
/// <summary> /// <summary>
/// Drops a collection, use very carefully. /// Drops a collection, use very carefully.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
void DropCollection<TDocument>(); void DropCollection<TDocument>();
/// <summary>
/// Drops a collection having a partitionkey, use very carefully.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
void DropCollection<TDocument>(string partitionKey);
} }
} }
+14 -7
View File
@@ -23,13 +23,6 @@ namespace MongoDbGenericRepository.Models
[BsonId] [BsonId]
public Guid Id { get; set; } public Guid Id { get; set; }
/// <summary>
/// The name of the property used for partitioning the collection
/// This will not be inserted into the collection.
/// This partition key will be prepended to the collection name to create a new collection.
/// </summary>
public string PartitionKey { get; set; }
/// <summary> /// <summary>
/// The datetime in UTC at which the document was added. /// The datetime in UTC at which the document was added.
/// </summary> /// </summary>
@@ -40,4 +33,18 @@ namespace MongoDbGenericRepository.Models
/// </summary> /// </summary>
public int Version { get; set; } public int Version { get; set; }
} }
public class PartitionedDocument : Document, IPartitionedDocument
{
public PartitionedDocument(string partitionKey)
{
PartitionKey = partitionKey;
}
/// <summary>
/// The name of the property used for partitioning the collection
/// This will not be inserted into the collection.
/// This partition key will be prepended to the collection name to create a new collection.
/// </summary>
public string PartitionKey { get; set; }
}
} }
+15 -1
View File
@@ -2,11 +2,25 @@
namespace MongoDbGenericRepository.Models namespace MongoDbGenericRepository.Models
{ {
/// <summary>
/// This class represents a basic document that can be stored in MongoDb.
/// Your document must implement this class in order for the MongoDbRepository to handle them.
/// </summary>
public interface IDocument public interface IDocument
{ {
DateTime AddedAtUtc { get; set; } DateTime AddedAtUtc { get; set; }
Guid Id { get; set; } Guid Id { get; set; }
string PartitionKey { get; set; }
int Version { get; set; } int Version { get; set; }
} }
/// <summary>
/// This class represents a document that can be inserted in a collection that can be partitioned.
/// The partition key allows for the creation of different collections having the same document schema.
/// This can be useful if you are planning to build a Software as a Service (SaaS) Platform, or if you want to reduce indexing.
/// You could for example insert Logs in different collections based on the week and year they where created, or their Log category/source.
/// </summary>
public interface IPartitionedDocument : IDocument
{
string PartitionKey { get; set; }
}
} }
+13 -9
View File
@@ -35,13 +35,13 @@ namespace MongoDbGenericRepository
} }
/// <summary> /// <summary>
/// The private GetCollection method /// Returns a collection for a document type that has a partition key.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <returns></returns> /// <param name="partitionKey">The value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(TDocument document) where TDocument : IDocument public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
{ {
return _database.GetCollection<TDocument>(PluralizePartitioned(document)); return _database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
} }
/// <summary> /// <summary>
@@ -53,14 +53,18 @@ namespace MongoDbGenericRepository
_database.DropCollection(Pluralize<TDocument>()); _database.DropCollection(Pluralize<TDocument>());
} }
/// <summary>
/// Drops a collection having a partitionkey, use very carefully.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
public void DropCollection<TDocument>(string partitionKey)
{
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
}
private string Pluralize<TDocument>() private string Pluralize<TDocument>()
{ {
return typeof(TDocument).Name.ToLower() + "s"; return typeof(TDocument).Name.ToLower() + "s";
} }
private string PluralizePartitioned<TDocument>(TDocument document) where TDocument : IDocument
{
return document.PartitionKey + typeof(TDocument).Name.ToLower() + "s";
}
} }
} }
+145 -112
View File
@@ -10,6 +10,41 @@ namespace MongoDbGenericRepository
{ {
public interface IBaseMongoRepository public interface IBaseMongoRepository
{ {
#region Create
/// <summary>
/// Asynchronously adds a document to the collection.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
void AddOne<TDocument>(TDocument document) where TDocument : IDocument;
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
#endregion
#region Read #region Read
/// <summary> /// <summary>
@@ -80,46 +115,16 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument; long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
#endregion Get #endregion Get
#region Create
/// <summary>
/// Asynchronously adds a document to the collection.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
void AddOne<TDocument>(TDocument document) where TDocument : IDocument;
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
#endregion
} }
/// <summary>
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
/// Its constructor must be given a connection string and a database name.
/// </summary>
public abstract class BaseMongoRepository : IBaseMongoRepository public abstract class BaseMongoRepository : IBaseMongoRepository
{ {
public string ConnectionString { get; set; } public string ConnectionString { get; set; }
@@ -137,6 +142,68 @@ namespace MongoDbGenericRepository
protected IMongoDbContext _mongoDbContext = null; protected IMongoDbContext _mongoDbContext = null;
#region Create
/// <summary>
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
{
FormatDocument(document);
await HandlePartitioned(document).InsertOneAsync(document);
}
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public void AddOne<TDocument>(TDocument document) where TDocument : IDocument
{
FormatDocument(document);
HandlePartitioned(document).InsertOne(document);
}
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{
if (!documents.Any())
{
return;
}
foreach (var doc in documents)
{
FormatDocument(doc);
}
await HandlePartitioned(documents.FirstOrDefault()).InsertManyAsync(documents);
}
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{
foreach (var document in documents)
{
FormatDocument(document);
}
HandlePartitioned(documents.FirstOrDefault()).InsertMany(documents);
}
#endregion Create
#region Read #region Read
/// <summary> /// <summary>
@@ -227,6 +294,7 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partitionKey</param>
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument
{ {
return await GetCollection<TDocument>().CountAsync(filter); return await GetCollection<TDocument>().CountAsync(filter);
@@ -237,9 +305,10 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter) where TDocument : IDocument /// <param name="partitionKey">An optional partitionKey</param>
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
{ {
return GetCollection<TDocument>().Find(filter).Count(); return HandlePartitioned<TDocument>(partitionKey).Find(filter).Count();
} }
/// <summary> /// <summary>
@@ -272,78 +341,6 @@ namespace MongoDbGenericRepository
#endregion Get #endregion Get
#region Create
private void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument
{
if (document.Id == default(Guid))
{
document.Id = Guid.NewGuid();
}
if (document.AddedAtUtc == default(DateTime))
{
document.AddedAtUtc = DateTime.UtcNow;
}
}
/// <summary>
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument
{
FormatDocument(document);
await GetCollection<TDocument>().InsertOneAsync(document);
}
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public void AddOne<TDocument>(TDocument document) where TDocument : IDocument
{
FormatDocument(document);
GetCollection<TDocument>().InsertOne(document);
}
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{
foreach (var document in documents)
{
FormatDocument(document);
}
await GetCollection<TDocument>().InsertManyAsync(documents);
}
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="document">The document you want to add.</param>
public void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument
{
foreach (var document in documents)
{
FormatDocument(document);
}
GetCollection<TDocument>().InsertMany(documents);
}
#endregion Create
#region Delete #region Delete
/// <summary> /// <summary>
@@ -509,13 +506,14 @@ namespace MongoDbGenericRepository
#endregion Find And Update #endregion Find And Update
/// <summary> /// <summary>
/// The private GetCollection method ///
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="partitionKey"></param>
/// <returns></returns> /// <returns></returns>
private IMongoCollection<TDocument> GetCollection<TDocument>(TDocument document) where TDocument : IDocument private IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
{ {
return _mongoDbContext.GetCollection<TDocument>(document); return _mongoDbContext.GetCollection<TDocument>(partitionKey);
} }
/// <summary> /// <summary>
@@ -527,5 +525,40 @@ namespace MongoDbGenericRepository
{ {
return _mongoDbContext.GetCollection<TDocument>(); return _mongoDbContext.GetCollection<TDocument>();
} }
private IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
{
if (document is IPartitionedDocument)
{
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
}
return GetCollection<TDocument>();
}
private IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey) where TDocument : IDocument
{
if (!string.IsNullOrEmpty(partitionKey))
{
return GetCollection<TDocument>(partitionKey);
}
return GetCollection<TDocument>();
}
private void FormatDocument<TDocument>(TDocument document) where TDocument : IDocument
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
if (document.Id == default(Guid))
{
document.Id = Guid.NewGuid();
}
if (document.AddedAtUtc == default(DateTime))
{
document.AddedAtUtc = DateTime.UtcNow;
}
}
} }
} }