How to add event listener via Fluent NHibernate?

2019-04-18 09:35发布

问题:

I want to add an event listener (IPreUpdateEventListener) to add NHibernate but I can't seem to find an example when using a fluent configuration.

I want to be able to add the listener when I create the session factory, e.g. when the following code is execute.

_sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connectionString).ShowSql())
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<IEntity>())
    .BuildSessionFactory();

Anyone know how to do this?

回答1:

Late answer, found your question when I was trying to do the same. Found a solution that should work:

_sessionFactory = Fluently.Configure()
   .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connectionString).ShowSql())
   .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entity>())
   .ExposeConfiguration(c => c.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] {new AuditEventListener()});


回答2:

So, late response, but for the sake of posterity, to add listeners without removing existing registration listeners (like the earlier answer from Bengt Be will do):

var config = new Configuration ();
config.AppendListeners (ListenerType.PreUpdate, new [] { new AuditEventListener () });

etc.



回答3:

Resurrecting the dead here but this:

........
   .ExposeConfiguration(c => c.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] {new AuditEventListener()});

Should be:

.ExposeConfiguration(c => c.AppendListeners(ListenerType.PreUpdate, new object[]
    {
        new AuditEventListener()
    });

I believe the 'SetListener' method (described in another answer) would also remove all previous listeners.

If you are into something a little more dynamic, you could do this:

private void AddListenerToConfiguration<T>(FluentConfiguration config, params ListenerType[] typesForListener)
        where T : class
    {
        var listener = Activator.CreateInstance<T>();

        config.ExposeConfiguration(x =>
            {
                foreach (var listenerType in typesForListener)
                {
                    x.AppendListeners(listenerType, new T[]
                    {
                        listener
                    });
                }
            });
    }

And then call something like this:

AddListenerToConfiguration<AuditEventListener>(smFactory, 
            ListenerType.PreUpdate);

This allows for cleaner code while you are looking at the Fluent configuration. It also allows you to easily register a single type to multiple listener types.

As for removing the default listeners, I wouldn't remove them unless I have a listener that inherits from the default listener implementation and calls base.METHODNAME on the overridden methods or replicates the same behavior as that of the default listeners.