Event Hookup Part Two

So what did I learn, that by creating a class that inherits EventArgs i can pass any information I need in an event, I did write a quick presenter and its test to ensure that the ITask interface worked as I expected, i.e. in exactly the same way as any other EventHandler interface definition that I have done.

The presenter Tests

   1: using Microsoft.VisualStudio.TestTools.UnitTesting;
   2: using Rhino.Mocks;
   3:  
   4: namespace ExamplePresenter.Tests
   5: {
   6:     [TestClass]
   7:     public class PresenterTests
   8:     {
   9:         private static MockRepository _mockery;
  10:         private ITask _mockTask;
  11:         private IView _mockView;
  12:         public TestContext TestContext { get; set; }
  13:  
  14:         [TestInitialize]
  15:         public void TestInitialize()
  16:         {
  17:             _mockery = new MockRepository();
  18:  
  19:             _mockView = _mockery.StrictMock<IView>();
  20:             _mockTask = _mockery.StrictMock<ITask>();
  21:         }
  22:  
  23:         [TestMethod]
  24:         public void Should_Hook_Up_Events_On_Creation()
  25:         {
  26:             using (_mockery.Record())
  27:             {
  28:                 _mockView.ButtonPressed += null;
  29:                 LastCall.IgnoreArguments();
  30:  
  31:                 _mockTask.ProgressMessage += null;
  32:                 LastCall.IgnoreArguments();
  33:             }
  34:  
  35:             using (_mockery.Playback())
  36:                 CreateSUT();
  37:         }
  38:  
  39:         private Presenter CreateSUT()
  40:         {
  41:             return new Presenter(_mockView, _mockTask);
  42:         }
  43:     }
  44: }

and the presenter code, all fairly standard

   1: using System;
   2:  
   3: namespace ExamplePresenter
   4: {
   5:     public class Presenter
   6:     {
   7:         private readonly ITask _task;
   8:         private readonly IView _view;
   9:  
  10:         public Presenter(IView view, ITask task)
  11:         {
  12:             _view = view;
  13:             _task = task;
  14:  
  15:             HookupEvents();
  16:         }
  17:  
  18:         private void HookupEvents()
  19:         {
  20:             _view.ButtonPressed += ButtonPressedEvent;
  21:             _task.ProgressMessage += ProgressMessageEvent;
  22:         }
  23:  
  24:         private void ButtonPressedEvent(object sender, EventArgs e)
  25:         {
  26:             throw new NotImplementedException();
  27:         }
  28:  
  29:         private void ProgressMessageEvent(object sender, MessageEventArgs args)
  30:         {
  31:             throw new NotImplementedException();
  32:         }
  33:     }
  34: }
  35:  
  36: using System;
  37:  
  38: namespace ExamplePresenter
  39: {
  40:     public interface IView
  41:     {
  42:         event EventHandler ButtonPressed;
  43:     }
  44: }

So why have I put all this here, just so I remember 🙂

Have Fun.

Unknown's avatar

About Duncan Butler

I’m Duncan, an entrepreneur and co-founder behind nth.digital and The Puzzle Board, where I combine heritage‑focused creativity with modern technology. My work spans AI development, C# engineering, and collaborative problem‑solving with Squad, building smarter tools, streamlined workflows, and engaging digital experiences. Whether refining puzzle production systems or exploring new AI capabilities, I’m driven by creating practical, intuitive solutions that make technology feel effortless.
This entry was posted in Programming. Bookmark the permalink.

Leave a comment