using MongoDB.Driver; using MongoDbGenericRepository.Attributes; using MongoDbGenericRepository.Utils; using System.Linq; using System.Reflection; using MongoDB.Bson; using MongoDB.Bson.Serialization.Serializers; using MongoDbGenericRepository.Internals; 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; } /// /// The constructor of the MongoDbContext, it needs an object implementing . /// /// An object implementing IMongoDatabase public MongoDbContext(IMongoDatabase mongoDatabase) { // Avoid legacy UUID representation: use Binary 0x04 subtype. InitializeGuidRepresentation(); 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) { InitializeGuidRepresentation(); Client = new MongoClient(connectionString); Database = Client.GetDatabase(databaseName); } /// /// Initialise an instance of a using a connection string /// /// public MongoDbContext(string connectionString) : this(connectionString, new MongoUrl(connectionString).DatabaseName) { } /// /// The constructor of the MongoDbContext, it needs a connection string and a database name. /// /// The MongoClient. /// The name of your database. public MongoDbContext(MongoClient client, string databaseName) { InitializeGuidRepresentation(); Client = client; Database = client.GetDatabase(databaseName); } /// /// Returns a collection for a document type. Also handles document types with a partition key. /// /// The type representing a Document. /// The optional value of the partition key. public virtual IMongoCollection GetCollection(string partitionKey = null) { return Database.GetCollection(GetCollectionName(partitionKey)); } /// /// Drops a collection, use very carefully. /// /// The type representing a Document. public virtual void DropCollection(string partitionKey = null) { Database.DropCollection(GetCollectionName(partitionKey)); } /// /// Sets the Guid representation of the MongoDB Driver. /// /// The new value of the GuidRepresentation public virtual void SetGuidRepresentation(GuidRepresentation guidRepresentation) { // GuidRepresentation and GuidRepresentationMode will be removed in the next major release of the MongoDB Driver. // We can safely replace this with RepositorySerializationProvider.Instance.RegisterSerializer once we upgrade to the next major release. #pragma warning disable CS0618 BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3; RepositorySerializationProvider.Instance.RegisterSerializer(new GuidSerializer(guidRepresentation)); #pragma warning restore CS0618 } /// /// Extracts the CollectionName attribute from the entity type, if any. /// /// The type representing a Document. /// The name of the collection in which the TDocument is stored. protected virtual string GetAttributeCollectionName() { return (typeof(TDocument).GetTypeInfo() .GetCustomAttributes(typeof(CollectionNameAttribute)) .FirstOrDefault() as CollectionNameAttribute)?.Name; } /// /// Initialize the Guid representation of the MongoDB Driver. /// Override this method to change the default GuidRepresentation. /// protected virtual void InitializeGuidRepresentation() { // by default, avoid legacy UUID representation: use Binary 0x04 subtype. SetGuidRepresentation(GuidRepresentation.Standard); } /// /// Given the document type and the partition key, returns the name of the collection it belongs to. /// /// The type representing a Document. /// The value of the partition key. /// The name of the collection. protected virtual string GetCollectionName(string partitionKey) { var collectionName = GetAttributeCollectionName() ?? Pluralize(); if (string.IsNullOrEmpty(partitionKey)) { return collectionName; } return $"{partitionKey}-{collectionName}"; } /// /// Very naively pluralizes a TDocument type name. /// /// The type representing a Document. /// The pluralized document name. protected virtual string Pluralize() { return (typeof(TDocument).Name.Pluralize()).Camelize(); } } }