Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46178

Where should we use events for non real time applications

I see all frameworks have their events implementation:

I see we can do all without any event, and I see we can heavily rely on it (as Symfony maintainers seem to tend for)

Are there common rules / global good practices for their use ?

Where should we [use|not use] events ?

I'm excluding the cases where we need to do real time interaction between server and client or user and application with javascript for example (also maybe I should only talk about web applications)

Upvotes: 0

Views: 64

Answers (1)

viktor77
viktor77

Reputation: 821

Examples:

  • Perform some time consuming tasks asynchronously, for example sending an email to a new user after signing up. You create a service that sends the email. This service is subscribed to an event which is fired after the response has been sent to the user. This way you don't make user wait until email has been sent.
  • You need to extend the functionality of a library. You can write listeners that perform some actions when certain events are fired. This way you can modify the default behaviour without having to modify original source code. When you update the library your code keeps working.

The main disadvantage in my opinion is that event driven code is harder to debug. You must do some research to find out which listeners have been called and the order in which they have been called.

Upvotes: 1

Related Questions