Update MongoDbContext.cs
Added a MongoDbContext ctor that takes a MongoClient for custom setup.
This commit is contained in:
committed by
GitHub
parent
ffc7e65b7b
commit
2d866d966c
@@ -12,17 +12,17 @@ namespace MongoDbGenericRepository
|
|||||||
public class MongoDbContext : IMongoDbContext
|
public class MongoDbContext : IMongoDbContext
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IMongoClient from the official MongoDb driver
|
/// The IMongoClient from the official MongoDB driver
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IMongoClient Client { get; }
|
public IMongoClient Client { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The IMongoDatabase from the official Mongodb driver
|
/// The IMongoDatabase from the official MongoDB driver
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IMongoDatabase Database { get; }
|
public IMongoDatabase Database { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the Guid representation of the MongoDb Driver.
|
/// Sets the Guid representation of the MongoDB Driver.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||||
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||||
@@ -31,17 +31,17 @@ namespace MongoDbGenericRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the Guid representation of the MongoDb Driver.
|
/// Initialize the Guid representation of the MongoDB Driver.
|
||||||
/// Override this method to change the default GuidRepresentation.
|
/// Override this method to change the default GuidRepresentation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void InitializeGuidRepresentation()
|
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;
|
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>.
|
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
|
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
|
||||||
public MongoDbContext(IMongoDatabase mongoDatabase)
|
public MongoDbContext(IMongoDatabase mongoDatabase)
|
||||||
@@ -64,6 +64,18 @@ namespace MongoDbGenericRepository
|
|||||||
Database = Client.GetDatabase(databaseName);
|
Database = Client.GetDatabase(databaseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor of the MongoDbContext, it needs a connection string and a database name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The MongoClient.</param>
|
||||||
|
/// <param name="databaseName">The name of your database.</param>
|
||||||
|
public MongoDbContext(MongoClient client, string databaseName)
|
||||||
|
{
|
||||||
|
InitializeGuidRepresentation();
|
||||||
|
Client = client;
|
||||||
|
Database = client.GetDatabase(databaseName);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts the CollectionName attribute from the entity type, if any.
|
/// Extracts the CollectionName attribute from the entity type, if any.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -96,7 +108,7 @@ namespace MongoDbGenericRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
|
|||||||
Reference in New Issue
Block a user