1

I'm trying to understand when it is appropriate to use events. It seems to me that an event can be thought of as an SQL trigger. Then, is it correct to use an event triggered by one domain object to modify another, or will it imply lack of a well thought out design if we use events to modify other object's states? Or should there be a mediator class through which these objects should modify each other? How do I decide?

Are there any tradeoffs that I should be concerned about here, like how use of events will affect testability?

2
  • Well, I am only learning about this site... and yes, your answer was useful so I'll upvote it. Thanks. Jan 27, 2010 at 10:53
  • Yes, I saw that on your profile. We all need to learn things from time to time :-)
    – KLE
    Jan 27, 2010 at 11:03

1 Answer 1

2

Events are designed to decouple one area from another.

This sometimes involves some asynchronous behavior, which can be an additional feature, but is not mandatory. For example, if you want to provide rapid feedback to the user in a GUI, and a part of your code runs too slowly (unless it needs to be finished before providing feedback), then the regular call can execute fast codes, and create an event for the others, then provide GUI feedback without waiting for the event to be actually processed. This event is stored in a Queue, and one or more threads are processing that Queue at their own pace.

For synchronous events, it is really useful for inter-module communication, where the two modules don't have compile-time dependencies on each other. Both can know about an event class, and an "event router" :

  • one module creates an event and call the router,
  • the router knows (from previous configuration) what other module should receive it, and send it to the receiving module.

Neither module know about the other, thus the decoupling concept. Very good if both must be maintained separately :-)


Many variations exist, for some topics like:

  • broadcasting to many receivers
  • fail-over (if the receiver is down temporarily, it starts again ; the event will be delivered when the receiver is up and registered again)
  • auditing : a technical module can receive events targeted at other modules, and log them
  • ...

Modifying Domain objects via events seems a bit strange. Is the decoupling mentioned higher really justified?

However, I wouldn't give a definite opinion before understanding more precisely what you have in mind.

2
  • After reading your post, I think the use of events is not justified in my case. I am not sure I understand the part where you talk of "inter-module communication", but maybe that's another question. Jan 27, 2010 at 10:13
  • @Abhijeet OK for events. I tried to improve a bit the part you mentionned, but I agree with you: a full discussion of decoupling in a question on its own (and probably already exist on StackOverflow ! ;-) ).
    – KLE
    Jan 27, 2010 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.