alexandre-spieser / mongodb-generic-repository

An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
MIT License
315 stars 87 forks source link

The MongoDb accessor enable override #21

Closed eshkard closed 5 years ago

eshkard commented 5 years ago

Hei Alexandre, Current implementation of BaseMongoRepository does not allow to override MongoDb accessor properties (CUD) can you kindly add setter to: protected MongoDbUpdater MongoDbUpdater { get; } protected MongoDbEraser MongoDbEraser { get; } protected MongoDbCreator MongoDbCreator { get; } protected MongoDbIndexHandler MongoDbIndexHandler { get; }

or allow inject extended functionality from constructor.

The main purpose of extension is to add transaction (MongoDb 4) functionality without many re-wrights in to our existing repositories. ExtendedDataAccess.zip

alexandre-spieser commented 5 years ago

Hi eshkard, thanks for your message. Yes this is something that can be done. I can change them like so:

        private MongoDbUpdater _mongoDbUpdater;
        protected MongoDbUpdater MongoDbUpdater
        {
            get
            {
                if (_mongoDbUpdater != null) { return _mongoDbUpdater; }

                lock (_initLock)
                {
                    if (_mongoDbUpdater == null)
                    {
                        _mongoDbUpdater = new MongoDbUpdater(MongoDbContext);
                    }
                }

                return _mongoDbUpdater;
            }
            set
            {
                _mongoDbUpdater = value;
            }
        }

Would that be ok for you ? Thanks for the zip file, I will integrate them to the base data accessor and write tests when I have the time :) In the mean time feel free to submit a PR with those changes! As long as they are integration-tested I'm happy to merge :).

alexandre-spieser commented 5 years ago

I have just updated the code. You can also extend functionalityof your repo without extending the accessors too:

        public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
                where TDocument : IDocument<TKey>
                where TKey : IEquatable<TKey>
        {
            var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
            var updateRes = await MongoDbUpdater.HandlePartitioned<TDocument, TKey>(modifiedDocument)
                    .ReplaceOneAsync(session, filter, modifiedDocument, cancellationToken: cancellationToken)
                    .ConfigureAwait(false);
            return updateRes.ModifiedCount == 1;
        }
alexandre-spieser commented 5 years ago

Done and released. https://github.com/alexandre-spieser/mongodb-generic-repository/releases/tag/1.4.0

eshkard commented 5 years ago

Grate thank's.

eshkard commented 5 years ago

By the way forget to send you Base repository extension code ExtendedBaseMongoRepository.zip With simple refactor (aka rename of some properties ) you can integrate it with-in package

Thank's

alexandre-spieser commented 5 years ago

cool will do, cheers!