diff --git a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs
index 2af5eb1..2a05c8d 100644
--- a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs
+++ b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs
@@ -24,29 +24,13 @@ namespace MongoDbGenericRepository
///
///
/// The value of the partition key.
- IMongoCollection GetCollection(string partitionKey = null) where TDocument : IDocument;
-
- ///
- /// Returns a collection for a document type that has a partition key.
- ///
- /// The type representing a Document.
- /// The type of the primary key for a Document.
- /// The value of the partition key.
- IMongoCollection GetCollection(string partitionKey = null)
- where TDocument : IDocument
- where TKey : IEquatable;
-
- ///
- /// Drops a collection, use very carefully.
- ///
- ///
- void DropCollection();
+ IMongoCollection GetCollection(string partitionKey = null);
///
/// Drops a collection having a partitionkey, use very carefully.
///
///
- void DropCollection(string partitionKey);
+ void DropCollection(string partitionKey = null);
///
/// Sets the Guid representation of the MongoDb Driver.
diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs
index 8771746..db18b18 100644
--- a/MongoDbGenericRepository/MongoDbContext.cs
+++ b/MongoDbGenericRepository/MongoDbContext.cs
@@ -23,27 +23,33 @@ namespace MongoDbGenericRepository
///
public IMongoDatabase Database { get; }
- static MongoDbContext()
- {
- // Avoid legacy UUID representation: use Binary 0x04 subtype.
- MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
- }
-
///
/// Sets the Guid representation of the MongoDb Driver.
///
/// The new value of the GuidRepresentation
- public void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
+ public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
{
MongoDefaults.GuidRepresentation = guidRepresentation;
}
+ ///
+ /// 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.
+ MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
+ }
+
///
/// The constructor of the MongoDbContext, it needs a an object implementing .
///
/// An object implementing IMongoDatabase
public MongoDbContext(IMongoDatabase mongoDatabase)
{
+ // Avoid legacy UUID representation: use Binary 0x04 subtype.
+ InitializeGuidRepresentation();
Database = mongoDatabase;
Client = Database.Client;
}
@@ -55,6 +61,7 @@ namespace MongoDbGenericRepository
/// The name of your database.
public MongoDbContext(string connectionString, string databaseName)
{
+ InitializeGuidRepresentation();
Client = new MongoClient(connectionString);
Database = Client.GetDatabase(databaseName);
}
@@ -72,52 +79,32 @@ namespace MongoDbGenericRepository
}
///
- /// Returns a collection for a document type that has a partition key.
+ /// Returns a collection for a document type. Also handles document types with a partition key.
///
/// The type representing a Document.
/// The value of the partition key.
- public IMongoCollection GetCollection(string partitionKey = null) where TDocument : IDocument
+ public IMongoCollection GetCollection(string partitionKey = null)
{
- if (string.IsNullOrEmpty(partitionKey))
- {
- return Database.GetCollection(GetAttributeCollectionName() ?? Pluralize());
- }
- return Database.GetCollection(partitionKey + "-" + GetAttributeCollectionName() ?? Pluralize());
- }
-
- ///
- /// Returns a collection for a document type that has a partition key.
- ///
- /// The type representing a Document.
- /// The type of the primary key for a Document.
- /// The value of the partition key.
- public IMongoCollection GetCollection(string partitionKey)
- where TDocument : IDocument
- where TKey : IEquatable
- {
- if (string.IsNullOrEmpty(partitionKey))
- {
- return Database.GetCollection(GetAttributeCollectionName() ?? Pluralize());
- }
- return Database.GetCollection(partitionKey + "-" + GetAttributeCollectionName() ?? Pluralize());
+ return Database.GetCollection(GetCollectionName(partitionKey));
}
///
/// Drops a collection, use very carefully.
///
/// The type representing a Document.
- public void DropCollection()
+ public void DropCollection(string partitionKey = null)
{
- Database.DropCollection(GetAttributeCollectionName() ?? Pluralize());
+ Database.DropCollection(GetCollectionName(partitionKey));
}
- ///
- /// Drops a collection having a partitionkey, use very carefully.
- ///
- /// The type representing a Document.
- public void DropCollection(string partitionKey)
+ private string GetCollectionName(string partitionKey)
{
- Database.DropCollection(partitionKey + "-" + GetAttributeCollectionName() ?? Pluralize());
+ if (string.IsNullOrEmpty(partitionKey))
+ {
+ return GetAttributeCollectionName() ?? Pluralize();
+ }
+ var collectionName = $"{partitionKey}-{(GetAttributeCollectionName() ?? Pluralize())}";
+ return collectionName;
}
///
diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs
index 5dd5ac2..8fadda8 100644
--- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs
+++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs
@@ -595,7 +595,7 @@ namespace MongoDbGenericRepository
where TDocument : IDocument
where TKey : IEquatable
{
- return MongoDbContext.GetCollection(partitionKey);
+ return MongoDbContext.GetCollection(partitionKey);
}
///