1
votes

I am new to Castle Windsor and Fluent NHibernate (AutoMapping).
I am currently working on multi Tenant application and have following requirement.

  1. Master Database to store tenant information
  2. Database per tenant

I have integrated the persistence facility using castle Windsor and I am able to access the Master Database. I am getting stuck where in I want to get the connection info for the tenant from the master Db, pass this information to the persistence facility again to build new session. So, eventually can have access to master db and tenant specific Db.

Can any one guide me how can I proceed on this.

In short:
On user log in, I want to validate the user and identify its tenant Database connection string from master db and use this info to build new session for tenant specific db operations.

1

1 Answers

0
votes

I would create 2 derived classes from ISession: IMasterSession and ITenantSession. This allows you to easily refer to either database from your controllers or view models.

You will need to make two registrations for ISessionFactory 1 for your master database and 1 for your tenant database. Name these so that you can refer to these.

Now register IMasterSession as follows:

Component.For<ISession>().PerWebRequest().Named("masterSession").UsingFactoryMethod((k, m) =>
    {
        var masterFactory = k.Resolve<ISessionFactory>("masterSessionFactory");
        return masterFactory.OpenSession();
    }),
Component.For<object>().Forward<IMasterSession>().Proxy.MixIns(registration =>
    registration.Component("masterSession"))

The session factories must be registered with lifestyle singleton, since you will only need one in your application. The lifetime of your session will depend on the kind of application you are making, probably PerWebRequest.

There might be a better way to do this, and I would love to hear that.

The approach for the tenant database should similar. However you would either need to use the overload of OpenSession that takes a IDBConnection or Implement a custom connection provider. You can find more about that here

I hope this helps.

Kind regards,

Marwijn.