locked
About the Orchard Event Model? RRS feed

  • Question

  • What is different about IEventHandler interface and IEvents interface? I want to communicate between modules, which should be used?
    Thursday, August 5, 2010 3:02 AM

Answers

  • I can't answer the difference between IEvents and IEventHandler, we should ask Louis I suppose, though I can explain you how to use IEventHandler to send messages to Orchard event bus.

    First you need to define an interface desribing the messages you want to send, like this:

    public interface IFeatureEventHandler : IEventHandler {
      void Install(Feature feature);
      void Enable(Feature feature);
      void Disable(Feature feature);
      void Uninstall(Feature feature);
    }
    
    

    Then in the class which has to send messages, add a declaration of this type in the constructor:

    public class MyService: IMyService {
      
      public MyCustomClass(IFeatureEventHandler featureEventHandler) {
        _featureEventHandler = featureEventHandler;
      }
    
      public MyMethod() {
        _featureEventHandler.Install(...);
      }
    }
    

    As you can see, we don't have to use an IEnumerable<IFeatureEventHandler> in the constructor. The event bus will take it to his own and call all the classes implementing this interface.

    You can look at the interface I have used here as an example. It is implemented by modules for instance to detect it is installed or enabled.

    Feel free to ask more questions if you can't make it work.

    Sebastien

    Thursday, August 5, 2010 4:59 AM
  • IEvents is for implementing generic handlers of events. For example, you could build your own implementation that does logging of everything that goes on the event bus. IEventHandler is, as Sebastien said, used to define more specialized specifications of an event interface for a specific purpose.

    In doubt, implement IEventHandler.

    Friday, August 6, 2010 10:27 PM

All replies

  • I can't answer the difference between IEvents and IEventHandler, we should ask Louis I suppose, though I can explain you how to use IEventHandler to send messages to Orchard event bus.

    First you need to define an interface desribing the messages you want to send, like this:

    public interface IFeatureEventHandler : IEventHandler {
      void Install(Feature feature);
      void Enable(Feature feature);
      void Disable(Feature feature);
      void Uninstall(Feature feature);
    }
    
    

    Then in the class which has to send messages, add a declaration of this type in the constructor:

    public class MyService: IMyService {
      
      public MyCustomClass(IFeatureEventHandler featureEventHandler) {
        _featureEventHandler = featureEventHandler;
      }
    
      public MyMethod() {
        _featureEventHandler.Install(...);
      }
    }
    

    As you can see, we don't have to use an IEnumerable<IFeatureEventHandler> in the constructor. The event bus will take it to his own and call all the classes implementing this interface.

    You can look at the interface I have used here as an example. It is implemented by modules for instance to detect it is installed or enabled.

    Feel free to ask more questions if you can't make it work.

    Sebastien

    Thursday, August 5, 2010 4:59 AM
  • Oh, I see IEventHandler purposes, thanks. However, I still want to know under what circumstances, we need to use IEvents derived our events?

    Thursday, August 5, 2010 2:14 PM
  • IEvents is for implementing generic handlers of events. For example, you could build your own implementation that does logging of everything that goes on the event bus. IEventHandler is, as Sebastien said, used to define more specialized specifications of an event interface for a specific purpose.

    In doubt, implement IEventHandler.

    Friday, August 6, 2010 10:27 PM