Unit of work in mongodb and C#

前端 未结 3 1711
既然无缘
既然无缘 2021-02-01 03:33

I know that MongoDB is not supposed to support unit of work, etc. But I think it would be nice to implement the repository which would store only the intentions (similar to crit

3条回答
  •  野性不改
    2021-02-01 04:31

    If you are interested in an implementation similar to Rob Connery's and NBlog storage code but using the mongodb csharp driver 2.0 (that is asynchronous), you can look at:

    https://github.com/alexandre-spieser/mongodb-generic-repository

    You can then write a custom repository inheriting from BaseMongoRepository.

    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);
        }
    }
    

提交回复
热议问题