Event Subscription and Notification
public class Stuff
{
// Public Event to allow other classes to subscribe to.
public event EventHandler GetHtmlDone = delegate { };
public void GetHTML(string url)
{
//Do stuff
// Raise Event, which triggers all method subscribed to it!
this.GetHtmlDone(this, new EventArgs());
}
}
public class Other
{
public Other()
{
Stuff stuff = new Stuff();
// Subscribe to the event.
stuff.GetHtmlDone += new EventHandler(OnGetHtmlDone);
// Execute
stuff.GetHTML("someUrl");
}
void OnGetHtmlDone(object sender, EventArgs e)
{
//Do stuff with the result since it's done
}
}
Using this pattern allows many more subscribers.
You also do not tie the notifier, the Stuff
class, to the caller, the Other
class.
You either have subscribers or not, no difference to the Stuff
class.
The Stuff
class should not know about the subscriber, it should merely raise an event it exposes for subscription.
EDIT
As ctacke pointed out correctly in the comments, raising the event using this.GetHtmlDone(this, new EventArgs());
will cause an exception if nobody has subscribed.
I changed my code above to ensure the event can be raised safely at all times by initialising my eventhandler.
As I'm always using it (by raising it) I'm sure it is only good practise to always initialise what you are using.
I could have added a null check on the event handler but in my personal oppinion I do not agree with that having to be the responsibility of the stuff
class. I feel the event should always be raised as it is the "responsible" thing to do.
I found this thread on SO which kind of confirmed to me that it does not seem wrong to do so.
In addition I also run Code Analysis on that code to ensure I'm not breaking the CA1805 rule by initialising the EventHandler. CA1805 was not raised and no rules have been broken.
Using my car analogy from the comments, I believe not initializing the eventhandler and raising it all the time would be the same than saying "When turning a corner in your car only use your indicator if someone is watching and if not don't bother". You never know if anyone is watching, so you might as well make sure you always do it.
This is simply my personal preference. Anyone else, please do add the != null check at all times if that is how you prefere to do it.
Thank you so much for the comments ctacke and for pointing this out. I learned quite a lot from this.
I will have to go back to some of my projects now and update some code to make sure my libraries are not crashing if nobody subscribes to my events. I feel quite silly not ever having caught that in any of my Tests.