Compare commits

...

4 Commits

Author SHA1 Message Date
alexandre-spieser 5e187e0b1f Exposed core MongoDb driver objects and removed the AddedAtUtc property constraint from the IDocument interface. 2017-09-23 18:57:56 +00:00
Alexandre SPIESER b843f2de7f Update README.md 2017-09-18 14:54:57 +01:00
Alexandre SPIESER 90ba1e4fc6 Update README.md 2017-09-10 13:07:22 +01:00
Alexandre SPIESER 926099626d Update README.md 2017-09-10 13:06:39 +01:00
17 changed files with 142 additions and 61 deletions
@@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" />
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
<PackageReference Include="MongoDbGenericRepository" Version="1.2.0" />
<PackageReference Include="MongoDbGenericRepository" Version="1.2.1" />
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" />
<PackageReference Include="xunit.runner.console" Version="2.3.0-beta5-build3769" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-build3769" />
@@ -9,7 +9,7 @@ namespace IntegrationTests.Infrastructure
{
const string connectionString = "mongodb://localhost:27017";
private static readonly ITestRepository instance = new TestRepository(connectionString, "MongoDbTests");
private static readonly ITestRepository _instance = new TestRepository(connectionString, "MongoDbTests");
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
@@ -26,7 +26,7 @@ namespace IntegrationTests.Infrastructure
{
get
{
return instance;
return _instance;
}
}
+5 -5
View File
@@ -31,16 +31,16 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="MongoDB.Bson, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Bson.dll</HintPath>
<HintPath>..\packages\MongoDbGenericRepository.1.2.1\lib\net45\MongoDB.Bson.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Driver.dll</HintPath>
<HintPath>..\packages\MongoDbGenericRepository.1.2.1\lib\net45\MongoDB.Driver.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver.Core, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Driver.Core.dll</HintPath>
<HintPath>..\packages\MongoDbGenericRepository.1.2.1\lib\net45\MongoDB.Driver.Core.dll</HintPath>
</Reference>
<Reference Include="MongoDbGenericRepository, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDbGenericRepository.dll</HintPath>
<HintPath>..\packages\MongoDbGenericRepository.1.2.1\lib\net45\MongoDbGenericRepository.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
@@ -50,7 +50,7 @@
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<HintPath>..\packages\MongoDbGenericRepository.1.2.1\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
+1 -1
View File
@@ -3,7 +3,7 @@
<package id="MongoDB.Bson" version="2.4.4" targetFramework="net461" />
<package id="MongoDB.Driver" version="2.4.4" targetFramework="net461" />
<package id="MongoDB.Driver.Core" version="2.4.4" targetFramework="net461" />
<package id="MongoDbGenericRepository" version="1.2.0" targetFramework="net461" />
<package id="MongoDbGenericRepository" version="1.2.1" targetFramework="net461" />
<package id="NUnit" version="3.7.1" targetFramework="net461" />
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net461" />
+1 -1
View File
@@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntegrationTests", "Integra
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDbGenericRepository", "MongoDbGenericRepository\MongoDbGenericRepository.csproj", "{EFC776C4-2AF3-440C-BE80-3FBE335817A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -801,11 +801,6 @@ namespace MongoDbGenericRepository
{
document.Id = Guid.NewGuid();
}
if (document.AddedAtUtc == default(DateTime))
{
document.AddedAtUtc = DateTime.UtcNow;
}
}
#endregion
@@ -8,6 +8,16 @@ namespace MongoDbGenericRepository
/// </summary>
public interface IMongoDbContext
{
/// <summary>
/// The IMongoClient from the official MongoDb driver
/// </summary>
IMongoClient Client { get; }
/// <summary>
/// The IMongoDatabase from the official Mongodb driver
/// </summary>
IMongoDatabase Database { get; }
/// <summary>
/// The private GetCollection method
/// </summary>
@@ -8,10 +8,6 @@ namespace MongoDbGenericRepository.Models
/// </summary>
public interface IDocument
{
/// <summary>
/// The date and UTC time at which the document was added to the collection.
/// </summary>
DateTime AddedAtUtc { get; set; }
/// <summary>
/// The Guid, which must be decorated with the [BsonId] attribute
/// if you want the MongoDb C# driver to consider it to be the document ID.
+27 -10
View File
@@ -8,8 +8,15 @@ namespace MongoDbGenericRepository
/// </summary>
public class MongoDbContext : IMongoDbContext
{
private readonly IMongoClient _client;
private readonly IMongoDatabase _database;
/// <summary>
/// The IMongoClient from the official MongoDb driver
/// </summary>
public IMongoClient Client { get; }
/// <summary>
/// The IMongoDatabase from the official Mongodb driver
/// </summary>
public IMongoDatabase Database { get; }
static MongoDbContext()
{
@@ -17,15 +24,25 @@ namespace MongoDbGenericRepository
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
}
/// <summary>
/// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>.
/// </summary>
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
public MongoDbContext(IMongoDatabase mongoDatabase)
{
Database = mongoDatabase;
Client = Database.Client;
}
/// <summary>
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
/// </summary>
/// <param name="connectionString"></param>
/// <param name="databaseName"></param>
/// <param name="connectionString">The connections string.</param>
/// <param name="databaseName">The name of your database.</param>
public MongoDbContext(string connectionString, string databaseName)
{
_client = new MongoClient(connectionString);
_database = _client.GetDatabase(databaseName);
Client = new MongoClient(connectionString);
Database = Client.GetDatabase(databaseName);
}
/// <summary>
@@ -35,7 +52,7 @@ namespace MongoDbGenericRepository
/// <returns></returns>
public IMongoCollection<TDocument> GetCollection<TDocument>()
{
return _database.GetCollection<TDocument>(Pluralize<TDocument>());
return Database.GetCollection<TDocument>(Pluralize<TDocument>());
}
/// <summary>
@@ -45,7 +62,7 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">The value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
{
return _database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
return Database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
}
/// <summary>
@@ -54,7 +71,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument"></typeparam>
public void DropCollection<TDocument>()
{
_database.DropCollection(Pluralize<TDocument>());
Database.DropCollection(Pluralize<TDocument>());
}
/// <summary>
@@ -63,7 +80,7 @@ namespace MongoDbGenericRepository
/// <typeparam name="TDocument"></typeparam>
public void DropCollection<TDocument>(string partitionKey)
{
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
Database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
}
/// <summary>
@@ -2,7 +2,7 @@
<package >
<metadata>
<id>MongoDbGenericRepository</id>
<version>1.2</version>
<version>1.2.1</version>
<title>MongoDb Generic Repository</title>
<authors>Alexandre Spieser</authors>
<owners>Alexandre Spieser</owners>
@@ -10,7 +10,7 @@
<projectUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description>
<releaseNotes>.NET Core support added.</releaseNotes>
<releaseNotes>Exposed core MongoDb driver objects and removed the AddedAtUtc property constraint from the IDocument interface.</releaseNotes>
<copyright>Copyright 2017 (c) Alexandre Spieser. All rights reserved.</copyright>
<tags>MongoDb Repository Generic NoSql</tags>
</metadata>
@@ -561,6 +561,16 @@
This is the interface of the IMongoDbContext which is managed by the <see cref="T:MongoDbGenericRepository.BaseMongoRepository"/>.
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.IMongoDbContext.GetCollection``1">
<summary>
The private GetCollection method
@@ -618,11 +628,6 @@
Your document must implement this class in order for the MongoDbRepository to handle them.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.AddedAtUtc">
<summary>
The date and UTC time at which the document was added to the collection.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.Id">
<summary>
The Guid, which must be decorated with the [BsonId] attribute
@@ -673,12 +678,28 @@
The MongoDb context
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(MongoDB.Driver.IMongoDatabase)">
<summary>
The constructor of the MongoDbContext, it needs a an object implementing <see cref="T:MongoDB.Driver.IMongoDatabase"/>.
</summary>
<param name="mongoDatabase">An object implementing IMongoDatabase</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(System.String,System.String)">
<summary>
The constructor of the MongoDbContext, it needs a connection string and a database name.
</summary>
<param name="connectionString"></param>
<param name="databaseName"></param>
<param name="connectionString">The connections string.</param>
<param name="databaseName">The name of your database.</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.GetCollection``1">
<summary>
@@ -561,6 +561,16 @@
This is the interface of the IMongoDbContext which is managed by the <see cref="T:MongoDbGenericRepository.BaseMongoRepository"/>.
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.IMongoDbContext.GetCollection``1">
<summary>
The private GetCollection method
@@ -618,11 +628,6 @@
Your document must implement this class in order for the MongoDbRepository to handle them.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.AddedAtUtc">
<summary>
The date and UTC time at which the document was added to the collection.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.Id">
<summary>
The Guid, which must be decorated with the [BsonId] attribute
@@ -673,12 +678,28 @@
The MongoDb context
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(MongoDB.Driver.IMongoDatabase)">
<summary>
The constructor of the MongoDbContext, it needs a an object implementing <see cref="T:MongoDB.Driver.IMongoDatabase"/>.
</summary>
<param name="mongoDatabase">An object implementing IMongoDatabase</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(System.String,System.String)">
<summary>
The constructor of the MongoDbContext, it needs a connection string and a database name.
</summary>
<param name="connectionString"></param>
<param name="databaseName"></param>
<param name="connectionString">The connections string.</param>
<param name="databaseName">The name of your database.</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.GetCollection``1">
<summary>
@@ -561,6 +561,16 @@
This is the interface of the IMongoDbContext which is managed by the <see cref="T:MongoDbGenericRepository.BaseMongoRepository"/>.
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.IMongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.IMongoDbContext.GetCollection``1">
<summary>
The private GetCollection method
@@ -618,11 +628,6 @@
Your document must implement this class in order for the MongoDbRepository to handle them.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.AddedAtUtc">
<summary>
The date and UTC time at which the document was added to the collection.
</summary>
</member>
<member name="P:MongoDbGenericRepository.Models.IDocument.Id">
<summary>
The Guid, which must be decorated with the [BsonId] attribute
@@ -673,12 +678,28 @@
The MongoDb context
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Client">
<summary>
The IMongoClient from the official MongoDb driver
</summary>
</member>
<member name="P:MongoDbGenericRepository.MongoDbContext.Database">
<summary>
The IMongoDatabase from the official Mongodb driver
</summary>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(MongoDB.Driver.IMongoDatabase)">
<summary>
The constructor of the MongoDbContext, it needs a an object implementing <see cref="T:MongoDB.Driver.IMongoDatabase"/>.
</summary>
<param name="mongoDatabase">An object implementing IMongoDatabase</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.#ctor(System.String,System.String)">
<summary>
The constructor of the MongoDbContext, it needs a connection string and a database name.
</summary>
<param name="connectionString"></param>
<param name="databaseName"></param>
<param name="connectionString">The connections string.</param>
<param name="databaseName">The name of your database.</param>
</member>
<member name="M:MongoDbGenericRepository.MongoDbContext.GetCollection``1">
<summary>
+9 -9
View File
@@ -12,7 +12,7 @@ You are responsible for managing its lifetime, it is advised to setup this repos
Here is an example of repository usage, where the TestRepository is implementing 2 custom methods:
```
```csharp
public interface ITestRepository : IBaseMongoRepository
{
void DropTestCollection<TDocument>();
@@ -40,17 +40,17 @@ Here is an example of repository usage, where the TestRepository is implementing
The repository can be instantiated like so:
```
```csharp
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
```
## Adding documents
To add a document, its class must inherit from the `Document` class or implement the `IDocument` interface:
```
```csharp
public class MyDocument : Document
{
public ReadTestsDocument()
public MyDocument()
{
Version = 2; // you can bump the version of the document schema if you change it over time
}
@@ -60,7 +60,7 @@ To add a document, its class must inherit from the `Document` class or implement
The `IDocument` interface can be seen below:
```
```csharp
/// <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.
@@ -76,10 +76,10 @@ The `IDocument` interface can be seen below:
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.
To use partitioned collections, you must define your documents using the PartitionedDocument class, which implements the IPartitionedDocument interface:
```
```csharp
public class MyPartitionedDocument : PartitionedDocument
{
public CreateTestsPartitionedDocument(string myPartitionKey) : base(myPartitionKey)
public MyPartitionedDocument(string myPartitionKey) : base(myPartitionKey)
{
Version = 1;
}
@@ -90,14 +90,14 @@ To use partitioned collections, you must define your documents using the Partiti
This partitioned key will be used as a prefix to your collection name.
The collection name is derived from the name of the type of your document, is set to camel case, and is pluralized using a class taken from Humanizer (https://github.com/Humanizr/Humanizer).
```
```csharp
var myDoc = new MyPartitionedDocument("myPartitionKey");
_testRepository.AddOne(myDoc);
```
The above code will generate a collection named `myPartitionKey-myPartitionedDocuments`.
Please refer to the IntegrationTests project for more usage examples.
Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstandard2.0) projects for more usage examples.
## Author
**Alexandre Spieser**