views:

22

answers:

1

Hi,

I'm creating a multi-tenant ASP.NET application using NHibernate (shared schema). I had intended to use NHibernates Event Listeners to inspect/modify entities prior to saving to ensure that the current tenants ID was being saved against the entity.

The authentication token containing the tenant ID is managed higher up and passed down into the data layer.

My problem is that Event Listeners are configured against an NHibernate Configuration rather than a specific ISession. This means that there's no way to pass in any identifiers for it to use, although I can access the ISession that caused the event to fire.

How can I go about passing ISession specific data into the Event Listener?

+2  A: 

You might be able to do this with an IInterceptor implementation because ISessionFactory has an overloaded OpenSession method that accepts an IInterceptor. Usage would be something like:

var interceptor = new MyInterceptor(tenantId);
var session = factory.OpenSession(interceptor);

Unfortunately there's no overload that accepts listeners.

Jamie Ide
Thanks Jamie, this looks like it might do the trick, I'm going to try it out now.
Matt