cleanup. Removing redundant GetCollection and Drop collection method, removed static constructor, added a virtual initialization method.
This commit is contained in:
@@ -24,29 +24,13 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
/// <param name="partitionKey">The value of the partition key.</param>
|
||||||
IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument;
|
IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection for a document type that has a partition key.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
|
||||||
IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Drops a collection, use very carefully.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
|
||||||
void DropCollection<TDocument>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Drops a collection having a partitionkey, use very carefully.
|
/// Drops a collection having a partitionkey, use very carefully.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
void DropCollection<TDocument>(string partitionKey);
|
void DropCollection<TDocument>(string partitionKey = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the Guid representation of the MongoDb Driver.
|
/// Sets the Guid representation of the MongoDb Driver.
|
||||||
|
|||||||
@@ -23,27 +23,33 @@ namespace MongoDbGenericRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IMongoDatabase Database { get; }
|
public IMongoDatabase Database { get; }
|
||||||
|
|
||||||
static MongoDbContext()
|
|
||||||
{
|
|
||||||
// Avoid legacy UUID representation: use Binary 0x04 subtype.
|
|
||||||
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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 void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||||
{
|
{
|
||||||
MongoDefaults.GuidRepresentation = guidRepresentation;
|
MongoDefaults.GuidRepresentation = guidRepresentation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize the Guid representation of the MongoDb Driver.
|
||||||
|
/// Override this method to change the default GuidRepresentation.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual void InitializeGuidRepresentation()
|
||||||
|
{
|
||||||
|
// by default, avoid lefacy UUID representation: use Binary 0x04 subtype.
|
||||||
|
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 a 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)
|
||||||
{
|
{
|
||||||
|
// Avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||||
|
InitializeGuidRepresentation();
|
||||||
Database = mongoDatabase;
|
Database = mongoDatabase;
|
||||||
Client = Database.Client;
|
Client = Database.Client;
|
||||||
}
|
}
|
||||||
@@ -55,6 +61,7 @@ namespace MongoDbGenericRepository
|
|||||||
/// <param name="databaseName">The name of your database.</param>
|
/// <param name="databaseName">The name of your database.</param>
|
||||||
public MongoDbContext(string connectionString, string databaseName)
|
public MongoDbContext(string connectionString, string databaseName)
|
||||||
{
|
{
|
||||||
|
InitializeGuidRepresentation();
|
||||||
Client = new MongoClient(connectionString);
|
Client = new MongoClient(connectionString);
|
||||||
Database = Client.GetDatabase(databaseName);
|
Database = Client.GetDatabase(databaseName);
|
||||||
}
|
}
|
||||||
@@ -72,52 +79,32 @@ namespace MongoDbGenericRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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.
|
||||||
/// </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>
|
||||||
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument
|
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(partitionKey))
|
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
|
||||||
{
|
|
||||||
return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
|
||||||
}
|
|
||||||
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection for a document type that has a partition key.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
|
||||||
/// <param name="partitionKey">The value of the partition key.</param>
|
|
||||||
public IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey)
|
|
||||||
where TDocument : IDocument<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(partitionKey))
|
|
||||||
{
|
|
||||||
return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
|
||||||
}
|
|
||||||
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Drops a collection, use very carefully.
|
/// Drops a collection, use very carefully.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
public void DropCollection<TDocument>()
|
public void DropCollection<TDocument>(string partitionKey = null)
|
||||||
{
|
{
|
||||||
Database.DropCollection(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private string GetCollectionName<TDocument>(string partitionKey)
|
||||||
/// Drops a collection having a partitionkey, use very carefully.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
|
||||||
public void DropCollection<TDocument>(string partitionKey)
|
|
||||||
{
|
{
|
||||||
Database.DropCollection(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
if (string.IsNullOrEmpty(partitionKey))
|
||||||
|
{
|
||||||
|
return GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
|
||||||
|
}
|
||||||
|
var collectionName = $"{partitionKey}-{(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>())}";
|
||||||
|
return collectionName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -595,7 +595,7 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return MongoDbContext.GetCollection<TDocument, TKey>(partitionKey);
|
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user