first step to split function into separate classes for better reuse and easier maintenance.

This commit is contained in:
Alexandre SPIESER
2019-04-11 23:59:34 +01:00
parent 2f29efafe3
commit a7391e06f8
16 changed files with 2189 additions and 734 deletions
+35 -34
View File
@@ -21,24 +21,6 @@ namespace MongoDbGenericRepository
/// </summary>
public IMongoDatabase Database { get; }
/// <summary>
/// Sets the Guid representation of the MongoDB Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
public virtual void SetGuidRepresentation(MongoDB.Bson.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 legacy UUID representation: use Binary 0x04 subtype.
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
}
/// <summary>
/// The constructor of the MongoDbContext, it needs an object implementing <see cref="IMongoDatabase"/>.
@@ -76,24 +58,12 @@ namespace MongoDbGenericRepository
Database = client.GetDatabase(databaseName);
}
/// <summary>
/// Extracts the CollectionName attribute from the entity type, if any.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns>The name of the collection in which the TDocument is stored.</returns>
private string GetAttributeCollectionName<TDocument>()
{
return (typeof(TDocument).GetTypeInfo()
.GetCustomAttributes(typeof(CollectionNameAttribute))
.FirstOrDefault() as CollectionNameAttribute)?.Name;
}
/// <summary>
/// Returns a collection for a document type. Also handles document types with a partition key.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The optional value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
public virtual IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
{
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
}
@@ -102,18 +72,49 @@ namespace MongoDbGenericRepository
/// Drops a collection, use very carefully.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>(string partitionKey = null)
public virtual void DropCollection<TDocument>(string partitionKey = null)
{
Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
}
/// <summary>
/// Sets the Guid representation of the MongoDB Driver.
/// </summary>
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
public virtual void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
{
MongoDefaults.GuidRepresentation = guidRepresentation;
}
/// <summary>
/// Extracts the CollectionName attribute from the entity type, if any.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns>The name of the collection in which the TDocument is stored.</returns>
protected virtual string GetAttributeCollectionName<TDocument>()
{
return (typeof(TDocument).GetTypeInfo()
.GetCustomAttributes(typeof(CollectionNameAttribute))
.FirstOrDefault() as CollectionNameAttribute)?.Name;
}
/// <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 legacy UUID representation: use Binary 0x04 subtype.
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
}
/// <summary>
/// Given the document type and the partition key, returns the name of the collection it belongs to.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The value of the partition key.</param>
/// <returns>The name of the collection.</returns>
private string GetCollectionName<TDocument>(string partitionKey)
protected virtual string GetCollectionName<TDocument>(string partitionKey)
{
var collectionName = GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>();
if (string.IsNullOrEmpty(partitionKey))
@@ -128,7 +129,7 @@ namespace MongoDbGenericRepository
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns>The pluralized document name.</returns>
private string Pluralize<TDocument>()
protected virtual string Pluralize<TDocument>()
{
return (typeof(TDocument).Name.Pluralize()).Camelize();
}