From 2d866d966cd199726439a6070a45a96b875e11f3 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Tue, 4 Dec 2018 12:58:44 +0000 Subject: [PATCH] Update MongoDbContext.cs Added a MongoDbContext ctor that takes a MongoClient for custom setup. --- MongoDbGenericRepository/MongoDbContext.cs | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs index 02e1e96..175c4c4 100644 --- a/MongoDbGenericRepository/MongoDbContext.cs +++ b/MongoDbGenericRepository/MongoDbContext.cs @@ -12,17 +12,17 @@ namespace MongoDbGenericRepository public class MongoDbContext : IMongoDbContext { /// - /// The IMongoClient from the official MongoDb driver + /// The IMongoClient from the official MongoDB driver /// public IMongoClient Client { get; } /// - /// The IMongoDatabase from the official Mongodb driver + /// The IMongoDatabase from the official MongoDB driver /// public IMongoDatabase Database { get; } /// - /// Sets the Guid representation of the MongoDb Driver. + /// Sets the Guid representation of the MongoDB Driver. /// /// The new value of the GuidRepresentation public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation) @@ -31,17 +31,17 @@ namespace MongoDbGenericRepository } /// - /// Initialize the Guid representation of the MongoDb Driver. + /// Initialize the Guid representation of the MongoDB Driver. /// Override this method to change the default GuidRepresentation. /// protected virtual void InitializeGuidRepresentation() { - // by default, avoid lefacy UUID representation: use Binary 0x04 subtype. + // by default, avoid legacy UUID representation: use Binary 0x04 subtype. MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard; } /// - /// The constructor of the MongoDbContext, it needs a an object implementing . + /// The constructor of the MongoDbContext, it needs an object implementing . /// /// An object implementing IMongoDatabase public MongoDbContext(IMongoDatabase mongoDatabase) @@ -63,6 +63,18 @@ namespace MongoDbGenericRepository Client = new MongoClient(connectionString); Database = Client.GetDatabase(databaseName); } + + /// + /// The constructor of the MongoDbContext, it needs a connection string and a database name. + /// + /// The MongoClient. + /// The name of your database. + public MongoDbContext(MongoClient client, string databaseName) + { + InitializeGuidRepresentation(); + Client = client; + Database = client.GetDatabase(databaseName); + } /// /// Extracts the CollectionName attribute from the entity type, if any. @@ -96,10 +108,10 @@ namespace MongoDbGenericRepository } /// - /// Given the docmuent type and the partition key, returns the name of the collection it belongs to. + /// Given the document type and the partition key, returns the name of the collection it belongs to. /// /// The type representing a Document. - /// The value of the partition key. + /// The value of the partition key. /// The name of the collection. private string GetCollectionName(string partitionKey) { @@ -121,4 +133,4 @@ namespace MongoDbGenericRepository return (typeof(TDocument).Name.Pluralize()).Camelize(); } } -} \ No newline at end of file +}