NHibernate 多事件监听器
是否可以注册多个事件监听器?
目前我们使用 .ExposeConfiguration(AddSoftDelete) 来注册事件监听器,其中 AddSoftDelete 是一个注册监听器的类;
private static void AddSoftDelete(Configuration config)
{
config.SetListener(ListenerType.Delete, new SoftDeleteListener());
}
我们发现我们无法注册多个同一类型的事件监听器,即我们无法为“ListenerType.Delete”注册多个监听器。
是否可以在不覆盖任何现有侦听器的情况下注册新侦听器?
已解决...
已成功使用以下代码注册多个侦听器;
config.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
{
new Listener1(),
new Listener2()
};
对每个 ListenerType 重复此操作。
Is it possible to register multiple event listeners?
We currently register event listeners using .ExposeConfiguration(AddSoftDelete) in which AddSoftDelete is a class registering the listener;
private static void AddSoftDelete(Configuration config)
{
config.SetListener(ListenerType.Delete, new SoftDeleteListener());
}
We have found that we cannot register multiple event listeners of the same type, i.e. we cannot register more than one listener for "ListenerType.Delete".
Is it possible to register new listeners without overriding any existing ones?
Solved...
Have managed to register multiple listeners using the following code;
config.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
{
new Listener1(),
new Listener2()
};
Repeat for each ListenerType.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听众并不是真正的听众,他们是实现者。 “事件”只能有一种实现。
您可以实现一个侦听器,在其中可以插入多个实现。例如不同实体类型的实现。您可以将“事件”传递给每个实现,直到其中一个实现处理该事件(例如,当实现 ISoftDeletable 接口时,SoftDeleteImplementor 正在处理它)。您需要关心竞争的实现者(更多的人可能会处理它,您调用它们的顺序很重要)。
The listeners are not actually listeners, they are implementors. There could only be one implementation of an "event".
You could implement a listener where you could plug in several implementations. For instance an implementation for different entity types. You could pass the "event" to each implementation until one of them handles it (eg. when the
ISoftDeletable
interface is implemented, theSoftDeleteImplementor
is handling it). You need to care about competing implementors (more the one could be handling it, the order matters in which you call them).为什么需要注册多个ListenerType.Delete?
如果您在一种类型上有多个事件侦听器,那么您的应用程序将会出现一些性能问题。如果您想使用此侦听器处理不同的实体,请在 SoftDeleteListener 类中执行此操作。
Why is there a need to register more than one ListenerType.Delete?
If you've got multiple event listeners on one type, there will be some performance issues on your application. If you want to handle different entities with this listener, so do it in your SoftDeleteListener class.
我在我的代码中做了类似的事情。 NHibernate.Cfg.Configuration 对象上应该有一个 AppendListeners(ListenerType type, object[]listeners) 方法。
还有一个 SetListeners 方法,我认为它会替换侦听器列表,而不是添加到它。
I do something similar in my code. There should be an AppendListeners(ListenerType type, object[] listeners) method on the NHibernate.Cfg.Configuration object.
There's also a SetListeners method which I assume replaces the listener list instead of adding on to it.