This project is read-only.

Event bus changes - a little clarification

Topics: Core, Writing modules
May 27, 2013 at 7:43 PM
Edited May 27, 2013 at 8:05 PM
As there clearly is some misunderstanding about how the event bus works and performs, here is a small clarification. (A bit technical, so non-developers will not benefit much from reading further).

In 1.x branch (upcoming 1.7 Orchard version) we've made a few big improvements so now performance should be considered a non-issue when deciding on whether to use or not to use the event bus:
  1. Event bus is very fast now. We've done a lot of refactoring here. Calling a method on some event handler is as fast as calling a strongly-typed delegate (ie. Func<T1, ..., T>). The first time you call a method, a strongly-typed delegate is created and cached so the slow part happens only once per method per application lifetime. Previously the reflection was used at each call and it was slow indeed.
    If someone would like to read more about the performance differences between direct, reflection-based and strongly-typed delegate calls, here's a good read.
  2. Event bus does not iterate over all IEventHandlers. It resolves and iterates only over those that implement a specific interface containing the method being called, eg. IUserEventHandler. We use keyed/named registrations for event handlers and use Autofac's IIndex<K, V> inside the bus to retrieve the subset.
Hope that helps understanding event bus a bit more:)
May 29, 2013 at 7:41 AM
Edited May 29, 2013 at 9:18 AM
Hi Piotr,

May I suggest that you add and propagate the information that it is no more necessary to include lines as
IEnumerable<MyInheritedIEventHandler> to inject lists of event handler in Services and related technical classes.
Another consequence is that no more loop to iterate on the elements of these IEnumerable is necessary and will be also prone to errors.

To be complete on the subject, it should also be mentionned that using IEnumerable<MyOwnHandler> and the loops through elements are necessary if you write your own 'bus' and inherit your interface directly from IDependency.
In this configuration, Ioc correctly fills your IEnumerable with all the elements implementing your interface and you have to enumerate to trigger events on them.
Doing this you don't benefit from the Orchard Event Bus concerning exceptions handling and improved speed mentionned by Piotr.

@pszmyd I have a question concerning the optimization point 2: what about EventHandler Inheriting from another EventHandler, just like this
public class OpenAuthUserEventHandler : IUserEventHandler
is this class treated as an IUserEventHandler as it should ?
May 29, 2013 at 10:20 AM
"Treated as it should", please explain what you mean by that?
May 29, 2013 at 10:38 AM
Edited May 29, 2013 at 11:11 AM
In 'that' I am questioning if the mentionned indexing feature used, ie "keyed/named registrations for event handlers and use Autofac's IIndex<K, V> inside the bus to retrieve the subset", will retrieve classes inheriting from....

I am fearing it is not working from the tests I have done using OpenAuthUserEventHandler, but as it is very difficult to trace some 'factual' confirmations are welcome.
Jun 1, 2013 at 8:39 AM