I’m building a multi-tenant web application where for security concerns, we need to have one instance of the database per tenant. So I have a MainDB for authentication and many ClientDB for application data.
I am using Asp.net MVC with Ninject and Fluent nHibernate. I have already setup my SessionFactory/Session/Repositories using Ninject and Fluent nHibernate in a Ninject Module at the start of the application. My sessions are PerRequestScope, as are repositories.
My problem is now I need to instanciate a SessionFactory (SingletonScope) instance for each of my tenants whenever one of them connects to the application and create a new session and necessary repositories for each webrequest. I’m puzzled as to how to do this and would need a concrete example.
Here’s the situation.
Application starts : The user of TenantX enters his login info. SessionFactory of MainDB gets created and opens a session to the MainDB to authenticate the user. Then the application creates the auth cookie.
Tenant accesses the application : The Tenant Name + ConnectionString are extracted from MainDB and Ninject must construct a tenant specific SessionFactory (SingletonScope) for that tenant. The rest of the web request, all controllers requiring a repository will be inject with a Tenant specific session/repository based on that tenant’s SessionFactory.
How do I setup that dynamic with Ninject? I was originally using Named instance when I had multiple databases but now that the databases are tenant specific, I’m lost…
After further research I can give you a better answer.
Whilst it’s possible to pass a connection string to
ISession.OpenSessiona better approach is to create a customConnectionProvider. The simplest approach is to derive fromDriverConnectionProviderand override theConnectionStringproperty:Using FluentNHibernate you set the provider like so:
The ConnectionProvider is evaluated each time you open a session allowing you to connect to tenant specific databases in your application.
An issue with the above approach is that the SessionFactory is shared. This is not really a problem if you are only using the first level cache (since this is tied to the session) but is if you decide to enable the second level cache (tied to the SessionFactory).
The recommended approach therefore is to have a SessionFactory-per-tenant (this would apply to schema-per-tenant and database-per-tenant strategies).
Another issue often overlooked is that although the second level cache is tied to the SessionFactory, in some cases the cache space itself is shared (reference). This can be resolved by setting the “regionName” property of the provider.
Below is a working implementation of SessionFactory-per-tenant based on your requirements.
The
Tenantclass contains the information we need to set up NHibernate for the tenant:Since we’ll be storing a
Dictionary<Tenant, ISessionFactory>we implement theIEquatableinterface so we can evaluate the Tenant keys.The process of getting the current tenant is abstracted like so:
Finally the
NHibernateSessionSourcewhich manages the sessions:When we create an instance of
NHibernateSessionSourcewe set up a default SessionFactory to our “default” database.When
CreateSession()is called we get aISessionFactoryinstance. This will either be the default session factory (if the current tenant is null) or a tenant specific session factory. The task of locating the tenant specific session factory is performed by theGetSessionFactorymethod.Finally we call
OpenSessionon theISessionFactoryinstance we have obtained.Note that when we create a session factory we set the SessionFactory name (for debugging/profiling purposes) and cache region prefix (for the reasons mentioned above).
Our IoC tool (in my case StructureMap) wires everything up:
Here NHibernateSessionSource is scoped as a singleton and ISession per request.
Hope this helps.