开发者

How to properly configure NHibernate event listeners

开发者 https://www.devze.com 2022-12-09 17:59 出处:网络
I\'m tr开发者_StackOverflowying to use an event listener for the first time.All samples I\'ve seen show how easy it is to configure, but for some reason I\'m having trouble - it doesn\'t seem to be ca

I'm tr开发者_StackOverflowying to use an event listener for the first time. All samples I've seen show how easy it is to configure, but for some reason I'm having trouble - it doesn't seem to be called. I suspect I'm missing something obvious.

I've tried this:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="TerraCognita.LoanExpress">
        <!-- other stuff... -->
        <listener type="delete" class="Test.TestDeleteListener, MyDllName" />
    </session-factory>
</hibernate-configuration>

as well as:

<hibernate-configuration   xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="TerraCognita.LoanExpress">
        <!-- other stuff... -->
        <event type="delete">
            <listener class="Test.TestDeleteListener, MyDllName" />
        </event>
    </session-factory>
</hibernate-configuration>

When configuration is loaded and the session factory is built:

var cfg = new NHibernate.Cfg.Configuration();
cfg.AddAssembly("MyDllName");
sessionFactory = cfg.BuildSessionFactory();

After instantiation, cfg.EventListeners.DeleteEventListeners has a single entry, of type DefaultDeleteEventListener (as I'd expect). However, after cfg.AddAssembly is called, this is still the case - but given the configuration, I would expect that the DeleteEventListener should actually be of type TestDeleteListener.

In my unit test, the only way I can get my event listener working is by manually adding it to the session info:

var sess = GetSession();
sess.GetSessionImplementation().Listeners.DeleteEventListeners = 
    new NHibernate.Event.IDeleteEventListener[] { new TestDeleteListener() };

I know this shouldn't be required, and I should be able to configure it properly. Can anyone shine a light on what I'm doing wrong here?


Your configuration is ok, it's just that you overlooked to call cfg.Configure(). Change your initialization code to this and your fine:

var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly("MyDllName");
sessionFactory = cfg.BuildSessionFactory();
0

精彩评论

暂无评论...
验证码 换一张
取 消