views:

543

answers:

1

I'm trying 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?

A: 

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();
Thomas Weller
...but that isn't what I'm seeing. I'm *not* replacing the default event listener. As noted above, what I see when tracing through is that the event listener is always of type DefaultDeleteEventListener, not mine.
Remi Despres-Smyth
Hm, but there's nothing wrong with the configuration!Where did you look for the listener, on the session factory or on the session? It should be on the factory... Did you literally try the above code (via copy/paste)?
Thomas Weller
That's the problem - missing call to cfg.Configure(). If you want to edit your answer to that, I'll select it as correct.
Remi Despres-Smyth
Umh, the question "Did you forget to call cfg.Configure() ?" was there from the beginning... Look at the "P.S." at the bottom of my answer (I didn't edit it).
Thomas Weller
I see that, but the rest of the answer you had above is irrelevant to my question. I'd like you to clean it up and just keep the relevant bits for posterity, something like "config is fine, you're missing call to... (I'd edit it myself, but I don't have the karma.) If you'd rather not, I can add an answer myself that has only has that.
Remi Despres-Smyth
...and the start of your answer is misleading: "The problem might be, that your new listener should be added to NH's event system, but without replacing the defaults, which is what the above config actually does". As I already noted, what I had wasn't replacing the default event listener.
Remi Despres-Smyth
Right then, edited the answer. Is that now ok for you ?
Thomas Weller
Done and done. Thanks again for your help on this.
Remi Despres-Smyth