From c117bf2a7fee378f1e02199dea9b2023a7089ee2 Mon Sep 17 00:00:00 2001 From: Andrea Bertoldo Date: Mon, 5 Feb 2018 14:48:26 +0100 Subject: [PATCH] Add CollectionName attribute for allowing explicit naming of collections --- .../IBaseMongoRepository.cs | 0 .../{ => Abstractions}/IMongoDbContext.cs | 0 .../IReadOnlyMongoRepository.cs | 0 .../Attributes/CollectionNameAttribute.cs | 15 ++++++ MongoDbGenericRepository/MongoDbContext.cs | 49 ++++++++++++------- 5 files changed, 46 insertions(+), 18 deletions(-) rename MongoDbGenericRepository/{ => Abstractions}/IBaseMongoRepository.cs (100%) rename MongoDbGenericRepository/{ => Abstractions}/IMongoDbContext.cs (100%) rename MongoDbGenericRepository/{ => Abstractions}/IReadOnlyMongoRepository.cs (100%) create mode 100644 MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs diff --git a/MongoDbGenericRepository/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs similarity index 100% rename from MongoDbGenericRepository/IBaseMongoRepository.cs rename to MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs diff --git a/MongoDbGenericRepository/IMongoDbContext.cs b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs similarity index 100% rename from MongoDbGenericRepository/IMongoDbContext.cs rename to MongoDbGenericRepository/Abstractions/IMongoDbContext.cs diff --git a/MongoDbGenericRepository/IReadOnlyMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs similarity index 100% rename from MongoDbGenericRepository/IReadOnlyMongoRepository.cs rename to MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs diff --git a/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs b/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs new file mode 100644 index 0000000..233b548 --- /dev/null +++ b/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs @@ -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; + } + } +} diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs index fbfb93b..d0488a0 100644 --- a/MongoDbGenericRepository/MongoDbContext.cs +++ b/MongoDbGenericRepository/MongoDbContext.cs @@ -1,7 +1,10 @@ using MongoDB.Driver; +using MongoDbGenericRepository.Attributes; using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Utils; using System; +using System.Linq; +using System.Reflection; namespace MongoDbGenericRepository { @@ -63,17 +66,21 @@ namespace MongoDbGenericRepository /// public IMongoCollection GetCollection() { - return Database.GetCollection(Pluralize()); - } + var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; + var name = collectionNameAttribute?.Name ?? Pluralize(); + return Database.GetCollection(name); + } - /// - /// Returns a collection for a document type that has a partition key. - /// - /// The type representing a Document. - /// The value of the partition key. - public IMongoCollection GetCollection(string partitionKey) where TDocument : IDocument + /// + /// Returns a collection for a document type that has a partition key. + /// + /// The type representing a Document. + /// The value of the partition key. + public IMongoCollection GetCollection(string partitionKey) where TDocument : IDocument { - return Database.GetCollection(partitionKey +"-"+ Pluralize()); + var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; + var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize(); + return Database.GetCollection(name); } /// @@ -86,16 +93,20 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable { - return Database.GetCollection(partitionKey + "-" + Pluralize()); - } + var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; + var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize(); + return Database.GetCollection(name); + } - /// - /// Drops a collection, use very carefully. - /// - /// The type representing a Document. - public void DropCollection() + /// + /// Drops a collection, use very carefully. + /// + /// The type representing a Document. + public void DropCollection() { - Database.DropCollection(Pluralize()); + var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; + var name = collectionNameAttribute?.Name ?? Pluralize(); + Database.DropCollection(name); } /// @@ -104,7 +115,9 @@ namespace MongoDbGenericRepository /// The type representing a Document. public void DropCollection(string partitionKey) { - Database.DropCollection(partitionKey + "-" + Pluralize()); + var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; + var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize(); + Database.DropCollection(name); } ///