Event bubbling in C#

How to propagate an event from a low level class to a top level one:

  1.  
  2. public class TopLevel{
  3. public bool Bubbled { get; private set; }
  4. private MiddleLevel observable;
  5. public TopLevel(MiddleLevel observable){
  6. this.observable = observable;
  7. observable.Triggered += (s, e) => {
  8. Bubbled = true;
  9. };
  10. }
  11. }
  12. public class MiddleLevel{
  13. public event EventHandler Triggered;
  14. private BottomLevel observable;
  15. public MiddleLevel(BottomLevel observable){
  16. this.observable = observable;
  17. //One may be tempted to bubble like this:
  18. //observable.Triggered += Triggered;
  19. //However, Triggered is null unless there is already
  20. //a subscriber. This is a better approach:
  21.  
  22. observable.Triggered += (s, e) => {
  23. Triggered(s, e);
  24. };
  25. }
  26. }
  27. public class BottomLevel{
  28. public event EventHandler Triggered;
  29.  
  30. public void DoSomething(){
  31. Triggered(this, EventArgs.Empty);
  32. }
  33. }
  34.  
  35. [TestFixture]
  36. public class TestingEventBubbling {
  37. [Test]
  38. public void Bubbling(){
  39. var bottom = new BottomLevel();
  40. var middle = new MiddleLevel(bottom);
  41. var top = new TopLevel(middle);
  42.  
  43. bottom.DoSomething();
  44.  
  45. top.Bubbled.Should().BeTrue();
  46. }
  47. }
  48.  

Events can only be raised from within the declaring type. Unfortunately they can't be be passed in as arguments to methods. Only += and -= operators are allowed out of the declaring type. One way to stub out the event could be through inheritance:

  1.  
  2. public class BottomLevel{
  3. public virtual event EventHandler Triggered;
  4.  
  5. public void DoSomething(){
  6. Triggered(this, EventArgs.Empty);
  7. }
  8. }
  9.  
  10. public class StubbedBottomLevel : BottomLevel {
  11. public override event EventHandler Triggered;
  12.  
  13. public void RaiseEvent(){
  14. Triggered(this, EventArgs.Empty);
  15. }
  16. }
  17.  
  18. [TestFixture]
  19. public class TestingEventBubbling {
  20. [Test]
  21. public void BubblingWithStub(){
  22. var bottom = new StubbedBottomLevel();
  23. var middle = new MiddleLevel(bottom);
  24. var top = new TopLevel(middle);
  25.  
  26. bottom.RaiseEvent();
  27. //bottom.DoSomething(); will not throw the event!
  28.  
  29. top.Bubbled.Should().BeTrue();
  30. }
  31.  

But declaring the event as virtual and then overriding it, is very tricky: replacing the call to RaiseEvent to DoSomething, makes the test fail! Looks like events where not designed to be overridden. A better approach:

  1.  
  2. public class BottomLevel{
  3. public event EventHandler Triggered;
  4.  
  5. public virtual void DoSomething(){
  6. //SomeLogic would go here...
  7. Raise(EventArgs.Empty);
  8. }
  9. protected virtual void Raise(EventArgs args){
  10. Triggered(this, args);
  11. }
  12. }
  13.  
  14. public class StubbedBottomLevel : BottomLevel {
  15. public override void DoSomething(){
  16. Raise(EventArgs.Empty);
  17. }
  18. }
  19.  
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.