Skip to content

Multiple Databases Management

Daniel Gonzalez Garcia edited this page Feb 27, 2015 · 1 revision

Session management

A given IDocumentStore can give access to one or more databases.

For majority of scenarios, what is needed the most is a IDocumentSession (or its asynchronous counterpart); so it is very common to take a dependency on such interface and let your IOC container (if used) to provide instances of such interface. For lower-level operations, dependemcies upon IDatabaseCommands can also be taken.

Both abstractions operate on a single given database, so the developer faces a small challenge when multiple databases are to be used in the same application as a single interface cannot be used for different databases.

Database-specific abstractions

A solution I have used extensively is having an interface for an abstraction per database. E.g. in our system we are using two databases: myDb and yourDb. In such case we would create two interfaces in order to use the "session" abstraction:

public interface IMyDbSession : IDocumentSession { }

public interface IYourDbSession : IDocumentSession { }

Database-specifuc implementations

For each interface we need a concrete implementation to delegate the execution. In order to create such an implementation there are multiple strategies, each with its pros and cons.

The one that was implementedin rvn-izr is to have a single abstraction implementation, of which each database-specific implementation inherits from. Better explained in code: