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

Use BaseRepository and ReadOnlyMongoRepository with generic Key #12

Closed eshkard closed 5 years ago

eshkard commented 5 years ago

Hey Alexandre , First of all i would like to thanks you for excellent implementation of MongoDb repository (one of the best i have seen so far).

I would like you to suggest to split your current implemented BaseMongoRepository class to BaseRepository<TKey>, ReadOnlyMongoRepository<TKey> and make a GuidBaseRepository:BaseRepository<Guid> as a example of implementation,

Another thing please expose FormatDocument<TDocument> as virtual in order to let repository use more generic.

Since there is many existing projects (including our's) that currently have stored data with keys that is not Guid but (ObjectId,Int, string etc...) that would like to move to your Generic repository implementation. Current implementation exposes Guid (TKey) as Default key option. By hard enclosing <TDocument,TKey> with Guid extensions in BaseRepository you are making harder to use it, since it is not possible to override your default <TDocument,TKey> extensions in BaseRepository developer can by mistake use Guid TKey instead of desired.

And in order to overcome current behavior flow ( and expose it to simple developers) we had to virtually rewrite BaseRepository and ReadOnlyMongoRepository since exposure of key type on each interaction with repository is not desired.

Thank's, Shkard Evgeny

alexandre-spieser commented 5 years ago

Hi, that's a good suggestion, thanks. Yeah I did that for Documents with Id of type Guid by default, but wanted to leave a maximum of choice for devs. If you use different types for the Ids for your documents it means you will need several BaseRepository<TKey> instances. To avoid having too many connections open I would recommend sharing the same IMongoDbContext instance across all the BaseRepository<TKey> for every TKey in use. I will have a look at implementing BaseRepository<Guid> , if you already have some implementation feel free to submit a PR :).

eshkard commented 5 years ago

You are absolutely wright about connection sharing. Currently i have database connection (IMongoDatabase) instance shared across multiple repositories.

alexandre-spieser commented 5 years ago

Done. https://github.com/alexandre-spieser/mongodb-generic-repository/releases/tag/1.3.9