using MongoDB.Driver; using MongoDbGenericRepository.Attributes; using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Utils; using System; using System.Linq; using System.Reflection; namespace MongoDbGenericRepository { /// /// The MongoDb context /// public class MongoDbContext : IMongoDbContext { /// /// The IMongoClient from the official MongoDb driver /// public IMongoClient Client { get; } /// /// The IMongoDatabase from the official Mongodb driver /// 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) { MongoDefaults.GuidRepresentation = guidRepresentation; } /// /// The constructor of the MongoDbContext, it needs a an object implementing . /// /// An object implementing IMongoDatabase public MongoDbContext(IMongoDatabase mongoDatabase) { Database = mongoDatabase; Client = Database.Client; } /// /// The constructor of the MongoDbContext, it needs a connection string and a database name. /// /// The connections string. /// The name of your database. public MongoDbContext(string connectionString, string databaseName) { Client = new MongoClient(connectionString); Database = Client.GetDatabase(databaseName); } /// /// The private GetCollection method /// /// The type representing a Document. /// public IMongoCollection GetCollection() { 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 { var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; var name = partitionKey + "-" + 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 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 { 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() { var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; var name = collectionNameAttribute?.Name ?? Pluralize(); Database.DropCollection(name); } /// /// Drops a collection having a partitionkey, use very carefully. /// /// The type representing a Document. public void DropCollection(string partitionKey) { var collectionNameAttribute = typeof(TDocument).GetTypeInfo().GetCustomAttributes(typeof(CollectionNameAttribute)).FirstOrDefault() as CollectionNameAttribute; var name = partitionKey + "-" + collectionNameAttribute?.Name ?? Pluralize(); Database.DropCollection(name); } /// /// Very naively pluralizes a TDocument type name. /// /// The type representing a Document. /// private string Pluralize() { return (typeof(TDocument).Name.Pluralize()).Camelize(); } } }