Compare commits

...

6 Commits

Author SHA1 Message Date
Alexandre SPIESER e91c89f054 Update README.md 2017-11-01 08:27:59 +00:00
alexandre-spieser 657beeac99 The MongoDbRepository now has a constructor that takes an IMongoDatabase. 2017-11-01 08:18:29 +00:00
alexandre-spieser fbb07475a1 Added tests for IdentityUsers 2017-10-29 19:29:43 +00:00
Alexandre SPIESER 30ea910b9c Update README.md 2017-10-22 01:06:26 +01:00
Alexandre SPIESER aae71cddc3 Update README.md 2017-10-02 00:20:50 +01:00
Alexandre SPIESER ad2cd66a7d Update README.md 2017-10-01 22:47:37 +01:00
22 changed files with 213 additions and 32 deletions
@@ -5,11 +5,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" />
<PackageReference Include="MongoDB.Driver" Version="2.4.4" /> <PackageReference Include="MongoDB.Driver" Version="2.4.4" />
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" /> <PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="xunit.runner.console" Version="2.3.0-beta5-build3769" /> <PackageReference Include="xunit.runner.console" Version="2.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-build3769" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
</ItemGroup> </ItemGroup>
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class CreateTestsPartitionedDocument : PartitionedDocument public class CreateTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class CreateTestsDocument : Document public class CreateTestsDocument : Document
{ {
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class DeleteTestsPartitionedDocument : PartitionedDocument public class DeleteTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class DeleteTestsDocument : Document public class DeleteTestsDocument : Document
{ {
+77
View File
@@ -0,0 +1,77 @@
using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using Xunit;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
namespace CoreCoreIntegrationTests
{
public class MongoIdentityUser<TKey> : IdentityUser<TKey>, IDocument<TKey>
where TKey : IEquatable<TKey>
{
public int Version { get; set; }
}
public class IdentityUserTest : MongoIdentityUser<Guid>, IDocument<Guid>
{
public IdentityUserTest()
{
Id = Guid.NewGuid();
Version = 2;
}
public string SomeContent { get; set; }
}
public class IdentityUserTests : BaseMongoDbRepositoryTests<IdentityUserTest>
{
[Fact]
public void AddOne()
{
// Arrange
var document = new IdentityUserTest();
// Act
SUT.AddOne<IdentityUserTest, Guid>(document);
// Assert
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == document.Id);
Assert.Equal(1, count);
}
[Fact]
public async Task AddOneAsync()
{
// Arrange
var document = new IdentityUserTest();
// Act
await SUT.AddOneAsync<IdentityUserTest, Guid>(document);
// Assert
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == document.Id);
Assert.Equal(1, count);
}
[Fact]
public void AddMany()
{
// Arrange
var documents = new List<IdentityUserTest> { new IdentityUserTest(), new IdentityUserTest() };
// Act
SUT.AddMany<IdentityUserTest, Guid>(documents);
// Assert
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.Equal(2, count);
}
[Fact]
public async Task AddManyAsync()
{
// Arrange
var documents = new List<IdentityUserTest> { new IdentityUserTest(), new IdentityUserTest() };
// Act
await SUT.AddManyAsync<IdentityUserTest, Guid>(documents);
// Assert
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.Equal(2, count);
}
}
}
@@ -2,10 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System; using System;
namespace IntegrationTests.Infrastructure namespace CoreIntegrationTests.Infrastructure
{ {
public class BaseMongoDbRepositoryTests<T> : IDisposable where T : Document, new() public class BaseMongoDbRepositoryTests<T> : IDisposable where T : new()
{ {
public T CreateTestDocument() public T CreateTestDocument()
{ {
@@ -44,6 +44,7 @@ namespace IntegrationTests.Infrastructure
public void Init() public void Init()
{ {
MongoDbConfig.EnsureConfigured();
SUT = TestRepository.Instance; SUT = TestRepository.Instance;
} }
@@ -1,6 +1,6 @@
using MongoDbGenericRepository; using MongoDbGenericRepository;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public interface ITestRepository : IBaseMongoRepository public interface ITestRepository : IBaseMongoRepository
{ {
@@ -0,0 +1,43 @@
using CoreCoreIntegrationTests;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using System.Threading;
namespace CoreIntegrationTests.Infrastructure
{
internal static class MongoDbConfig
{
private static bool _initialized = false;
private static object _initializationLock = new object();
private static object _initializationTarget;
public static void EnsureConfigured()
{
EnsureConfiguredImpl();
}
private static void EnsureConfiguredImpl()
{
LazyInitializer.EnsureInitialized(ref _initializationTarget, ref _initialized, ref _initializationLock, () =>
{
Configure();
return null;
});
}
private static void Configure()
{
RegisterConventions();
}
private static void RegisterConventions()
{
var pack = new ConventionPack
{
new IgnoreIfNullConvention(false),
new CamelCaseElementNameConvention(),
};
}
}
}
@@ -1,6 +1,6 @@
using MongoDbGenericRepository; using MongoDbGenericRepository;
namespace IntegrationTests.Infrastructure namespace CoreIntegrationTests.Infrastructure
{ {
/// <summary> /// <summary>
/// A singleton implementation of the TestRepository /// A singleton implementation of the TestRepository
@@ -1,11 +1,11 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ProjectTestsPartitionedDocument : PartitionedDocument public class ProjectTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,11 +1,11 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class Nested public class Nested
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ReadTestsPartitionedDocument : PartitionedDocument public class ReadTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ReadTestsDocument : Document public class ReadTestsDocument : Document
{ {
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class UpdateTestsPartitionedDocument : PartitionedDocument public class UpdateTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class UpdateTestsDocument : Document public class UpdateTestsDocument : Document
{ {
@@ -900,6 +900,15 @@ namespace MongoDbGenericRepository
MongoDbContext = mongoDbContext; MongoDbContext = mongoDbContext;
} }
/// <summary>
/// The contructor taking a <see cref="IMongoDatabase"/>.
/// </summary>
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
protected BaseMongoRepository(IMongoDatabase mongoDatabase)
{
MongoDbContext = new MongoDbContext(mongoDatabase);
}
/// <summary> /// <summary>
/// The MongoDbContext /// The MongoDbContext
/// </summary> /// </summary>
@@ -2009,6 +2018,7 @@ namespace MongoDbGenericRepository
} }
/// <summary>
/// Groups filtered a collection of documents given a grouping criteria, /// Groups filtered a collection of documents given a grouping criteria,
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
/// </summary> /// </summary>
@@ -53,5 +53,11 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
void DropCollection<TDocument>(string partitionKey); void DropCollection<TDocument>(string partitionKey);
/// <summary>
/// Sets the Guid representation of the MongoDb Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation);
} }
} }
@@ -13,7 +13,6 @@ namespace MongoDbGenericRepository.Models
/// The Primary Key, which must be decorated with the [BsonId] attribute /// The Primary Key, which must be decorated with the [BsonId] attribute
/// if you want the MongoDb C# driver to consider it to be the document ID. /// if you want the MongoDb C# driver to consider it to be the document ID.
/// </summary> /// </summary>
[BsonId]
TKey Id { get; set; } TKey Id { get; set; }
/// <summary> /// <summary>
/// A version number, to indicate the version of the schema. /// A version number, to indicate the version of the schema.
@@ -25,6 +25,15 @@ namespace MongoDbGenericRepository
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard; MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
} }
/// <summary>
/// Sets the Guid representation of the MongoDb Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
public void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
{
MongoDefaults.GuidRepresentation = guidRepresentation;
}
/// <summary> /// <summary>
/// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>. /// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>.
/// </summary> /// </summary>
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Type="http://schemas.microsoft.com/packaging/2010/07/manifest" Target="/MongoDbGenericRepository.nuspec" Id="Rd7ba7aa97a7d4fb2" />
<Relationship Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="/package/services/metadata/core-properties/e5080d3094a649a39d247315f4dcd1ba.psmdcp" Id="Rcfffb2818c774b93" />
</Relationships>
+32 -3
View File
@@ -4,6 +4,8 @@ An example of generic repository implementation using the MongoDB C# Sharp 2.0 d
Now available as a nuget package: Now available as a nuget package:
https://www.nuget.org/packages/MongoDbGenericRepository/ https://www.nuget.org/packages/MongoDbGenericRepository/
Covered by 200+ integration tests and counting.
# Usage examples # Usage examples
This repository is meant to be inherited from. This repository is meant to be inherited from.
@@ -44,8 +46,16 @@ The repository can be instantiated like so:
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests"); ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
``` ```
If you prefer to reuse the same MongoDb database across you application, you can use the `MongoDatabase` from the MongoDb driver implementing the `IMongoDatabase` interface:
```csharp
var client = new MongoClient(connectionString);
var mongoDbDatabase = Client.GetDatabase(databaseName);
ITestRepository testRepository = new TestRepository(mongoDbDatabase);
```
## Adding documents ## Adding documents
To add a document, its class must inherit from the `Document` class or implement the `IDocument` interface: To add a document, its class must inherit from the `Document` class, implement the `IDocument` or `IDocument<TKey>` interface:
```csharp ```csharp
public class MyDocument : Document public class MyDocument : Document
@@ -58,7 +68,7 @@ To add a document, its class must inherit from the `Document` class or implement
} }
``` ```
The `IDocument` interface can be seen below: The `IDocument` and `IDocument<TKey>` interfaces can be seen below:
```csharp ```csharp
/// <summary> /// <summary>
@@ -70,7 +80,26 @@ The `IDocument` interface can be seen below:
Guid Id { get; set; } Guid Id { get; set; }
int Version { get; set; } int Version { get; set; }
} }
/// <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<TKey> where TKey : IEquatable<TKey>
{
/// <summary>
/// The Primary Key, which must be decorated with the [BsonId] attribute
/// if you want the MongoDb C# driver to consider it to be the document ID.
/// </summary>
[BsonId]
TKey Id { get; set; }
/// <summary>
/// A version number, to indicate the version of the schema.
/// </summary>
int Version { get; set; }
}
``` ```
## Partitioned collections ## Partitioned collections
This repository also allows you to partition your document across multiple collections, this can be useful if you are running a SaaS application and want to keep good performance. This repository also allows you to partition your document across multiple collections, this can be useful if you are running a SaaS application and want to keep good performance.
@@ -104,7 +133,7 @@ Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstanda
## Donations ## Donations
Feeling like my work is worth a coffee? Feeling like my work is worth a coffee?
Donations are welcome and will go towards further development of this project as well as other MongoDb related projects. Use the wallet address below to donate. Donations are welcome and will go towards further development of this project as well as other MongoDb related projects. Use the wallet address below to donate.
BTC Donations: 1Qc5ZpNA7g66KEEMcz7MXxwNyyoRyKJJZ BTC Donations: 1FDMWqSK8SHXDGKKp7gyZc4rknynWJ7qbj
*Thank you for your support and generosity!* *Thank you for your support and generosity!*