Merge pull request #2 from Etchelon/master
Add CollectionName attribute for allowing explicit naming of collections
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MongoDbGenericRepository.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
|
public class CollectionNameAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public CollectionNameAttribute(string name)
|
||||||
|
{
|
||||||
|
this.Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDbGenericRepository.Attributes;
|
||||||
using MongoDbGenericRepository.Models;
|
using MongoDbGenericRepository.Models;
|
||||||
using MongoDbGenericRepository.Utils;
|
using MongoDbGenericRepository.Utils;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace MongoDbGenericRepository
|
namespace MongoDbGenericRepository
|
||||||
{
|
{
|
||||||
@@ -63,7 +66,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IMongoCollection<TDocument> GetCollection<TDocument>()
|
public IMongoCollection<TDocument> GetCollection<TDocument>()
|
||||||
{
|
{
|
||||||
return Database.GetCollection<TDocument>(Pluralize<TDocument>());
|
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute;
|
||||||
|
var name = collectionNameAttribute?.Name ?? Pluralize<TDocument>();
|
||||||
|
return Database.GetCollection<TDocument>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -73,7 +78,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <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) where TDocument : IDocument
|
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
||||||
{
|
{
|
||||||
return Database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
|
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute;
|
||||||
|
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>();
|
||||||
|
return Database.GetCollection<TDocument>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -86,7 +93,9 @@ namespace MongoDbGenericRepository
|
|||||||
where TDocument : IDocument<TKey>
|
where TDocument : IDocument<TKey>
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
return Database.GetCollection<TDocument>(partitionKey + "-" + Pluralize<TDocument>());
|
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute;
|
||||||
|
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>();
|
||||||
|
return Database.GetCollection<TDocument>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -95,7 +104,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <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>()
|
||||||
{
|
{
|
||||||
Database.DropCollection(Pluralize<TDocument>());
|
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute;
|
||||||
|
var name = collectionNameAttribute?.Name ?? Pluralize<TDocument>();
|
||||||
|
Database.DropCollection(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -104,7 +115,9 @@ namespace MongoDbGenericRepository
|
|||||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||||
public void DropCollection<TDocument>(string partitionKey)
|
public void DropCollection<TDocument>(string partitionKey)
|
||||||
{
|
{
|
||||||
Database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
|
var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute;
|
||||||
|
var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize<TDocument>();
|
||||||
|
Database.DropCollection(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user