views:

286

answers:

1

I can't seem to find an example where someone added an interceptor via web.config - is this possible?

And yes I know about event listeners and will be using them on another project - but I wanted to see if I could get around having to inject the interceptor in code - thank you

+1  A: 

I don't think it's supported but you can easily fetch and instantiate interceptors from a custom config section:

NHibernate.Cfg.Configuration cfg = ...
var interceptors = (NameValueCollection) ConfigurationManager.GetSection("nhibernate.interceptors");
foreach (string k in interceptors)
    cfg.SetInterceptor((IInterceptor) Activator.CreateInstance(Type.GetType(k)));

web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
   <section name="nhibernate.interceptors" type="System.Configuration.NameValueSectionHandler, System" />
 </configSections>
 <nhibernate.interceptors>
    <add key="MyApp.Interceptors.SomeInterceptor, MyApp" value=""/>
    <add key="MyApp.Interceptors.AnotherInterceptor, MyApp" value=""/>
 </nhibernate.interceptors>
</configuration>
Mauricio Scheffer