### Define Document Class Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Example of a document class inheriting from the Document base class. ```csharp public class MyDocument : Document { public MyDocument() { Version = 2; // you can bump the version of the document schema if you change it over time } public string SomeContent { get; set; } } ``` -------------------------------- ### Instantiate Repository Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Shows how to instantiate the repository using a connection string and database name. ```csharp ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests"); ITestRepository testTKeyRepository = new TestTKeyRepository(connectionString); ``` -------------------------------- ### Instantiate Repository with IMongoDatabase Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Shows how to reuse an existing MongoDatabase instance from the MongoDB driver. ```csharp var client = new MongoClient(connectionString); var mongoDbDatabase = Client.GetDatabase(databaseName); ITestRepository testRepository = new TestRepository(mongoDbDatabase); ``` -------------------------------- ### Add a Partitioned Document Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Instantiate a partitioned document with a partition key and add it to the repository. The collection name will be prefixed with the provided partition key. ```csharp var myDoc = new MyPartitionedDocument("myPartitionKey"); _testRepository.AddOne(myDoc); ``` -------------------------------- ### Manage MongoDB Indexes with CreateTextIndexAsync Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Demonstrates creating a text index, verifying its existence, and dropping it using the repository's index management methods. ```csharp string expectedIndexName = $"myCustomIndexName"; var option = new IndexCreationOptions { Name = expectedIndexName }; // Act // create a text index against the Version property of the document. var result = await SUT.CreateTextIndexAsync(x => x.Version, option, PartitionKey); // Assert var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); Assert.Contains(expectedIndexName, listOfIndexNames); // Cleanup await SUT.DropIndexAsync(expectedIndexName, PartitionKey); ``` -------------------------------- ### Define a PartitionedDocument Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Implement the IPartitionedDocument interface by inheriting from PartitionedDocument to enable collection partitioning. ```csharp public class MyPartitionedDocument : PartitionedDocument { public MyPartitionedDocument(string myPartitionKey) : base(myPartitionKey) { Version = 1; } public string SomeContent { get; set; } } ``` -------------------------------- ### Repository with typed key using BaseMongoRepository Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Shows a TestTKeyRepository class that inherits BaseMongoRepository and implements ITestRepository, where TKey is constrained to IEquatable. It uses a hardcoded connection string and a static singleton instance. This variant is for documents that all share the same Id type. ```csharp public class TestTKeyRepository : BaseMongoRepository, ITestRepository where TKey : IEquatable { const string connectionString = "mongodb://localhost:27017/MongoDbTests"; private static readonly ITestRepository _instance = new TestTKeyRepository(connectionString); /// private TestTKeyRepository(string connectionString) : base(connectionString) { } } ``` -------------------------------- ### Custom repository interface and implementation with BaseMongoRepository Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Shows how to define an ITestRepository interface extending IBaseMongoRepository and implement it with TestRepository inheriting BaseMongoRepository. The implementation adds DropTestCollection methods that call MongoDbContext.DropCollection, with and without a partition key. The repository is meant to be inherited from and managed as a singleton. ```csharp public interface ITestRepository : IBaseMongoRepository { void DropTestCollection(); void DropTestCollection(string partitionKey); } public class TestRepository : BaseMongoRepository, ITestRepository { public TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName) { } public void DropTestCollection() { MongoDbContext.DropCollection(); } public void DropTestCollection(string partitionKey) { MongoDbContext.DropCollection(partitionKey); } } ``` -------------------------------- ### IDocument and IDocument Interfaces Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md The required interfaces for documents to be handled by the repository. ```csharp /// /// This class represents a basic document that can be stored in MongoDb. /// Your document must implement this class in order for the MongoDbRepository to handle them. /// public interface IDocument { Guid Id { get; set; } int Version { get; set; } } /// /// This class represents a basic document that can be stored in MongoDb. /// Your document must implement this class in order for the MongoDbRepository to handle them. /// public interface IDocument where TKey : IEquatable { /// /// The Primary Key, which must be decorated with the [BsonId] attribute /// if you want the MongoDb C# driver to consider it to be the document ID. /// [BsonId] TKey Id { get; set; } /// /// A version number, to indicate the version of the schema. /// int Version { get; set; } } ``` -------------------------------- ### Use CollectionName Attribute Source: https://github.com/alexandre-spieser/mongodb-generic-repository/blob/master/README.md Override the default collection naming convention by applying the CollectionName attribute to your document class. ```csharp [CollectionName("MyCollectionName")] public class MyDocument : Document { public MyDocument() { Version = 2; } public string SomeContent { get; set; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.