The repository by nature is coupled to mongo and I can't see a non contrived way of testing the repo without connecting to a mongo instance. So I have a test mongo session class which uses a different test db to the main session used for prod. I then have behaviour tests that write to mongo and then inspect mongo to check state. This is not considered good test practise as I end up using multiple pieces of repo functionality to insert then verify, but again I don't see a non contrivied alternative. This is at least easy to do, including clear down between tests as we don't have to worry about schema setup etc. Nor constraints etc.
Be interested in alternatives, but I find a lot of test experts lose touch with reality when they describe how to integration test repositories!
Regarding your life time question, are you writing an app or web site? I'm using asp.net and repositories (and by implication fresh sessions which are injected into repository constrictors) are created on every request and passed into the controllers constructor by ioc. This is standard practise for database access so don't see why mongo approach would be different? Remember here we don't have to do what an orm does in terms of managing a change set. Mongo is much lower level, especially if you want to work atomically.
Index is interesting one. I'm not sure there. Definitey not per session, but not sure what happens if one tries to create an index when collection not yet defined. If it works then I'd be inclined to do a 1 time setup script. If not, maybe a static constructor on repositories to do it one time per app domain?
Dan
Sent from my iPad
On 23 Aug 2011, at 13:46, YuriyHohan <yurii...@gmail.com> wrote:
> 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 criteria) and then commit them to the
> DB. Otherwise in every method in your repository you have to create
> connection to DB and then close it. If we place the connection to DBwriting