We could do this by hand sticking to the convention

Info icon This preview shows pages 200–203. Sign up to view the full content.

View Full Document Right Arrow Icon
base class. We could do this by hand, sticking to the convention of calling the first parameter sender and the data parameter e : delegate void ProcessEventHandler(object sender, ProcessEventArgs e); Once again, this is such a common thing to need that the framework provides us with a generic type, EventHandler<T> , to save us the boilerplate code. So we can replace the ProcessEventHandler with an EventHandler<ProcessEventArgs> . Let’s update our event declarations (see Example 5-25 ). Example 5-25. Updated event members public event EventHandler<ProcessEventArgs> Processing; public event EventHandler<ProcessEventArgs> Processed; and then our helper methods which raise the event that will need to take a ProcessE ventArgs (see Example 5-26 ). Example 5-26. Updated code for raising events private void OnProcessing(ProcessEventArgs e) { if (Processing != null) { Processing(this, e); } } private void OnProcessed(ProcessEventArgs e) { if (Processed != null) { Processed(this, e); } } And finally, our calls to those methods will need to create an appropriate ProcessEven tArgs object, as shown in Example 5-27 . Example 5-27. Creating the event arguments object public void Process(Document doc) { ProcessEventArgs e = new ProcessEventArgs(doc); OnProcessing(e); 176 | Chapter 5: Composability and Extensibility with Delegates
Image of page 200

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// First time, do the quick check foreach (ActionCheckPair process in processes) { if (process.QuickCheck != null && !process.QuickCheck(doc)) { Console.WriteLine("The process will not succeed."); if (LogTextProvider != null) { Console.WriteLine(LogTextProvider(doc)); } OnProcessed(e); return; } } // Then perform the action foreach (ActionCheckPair process in processes) { process.Action(doc); if (LogTextProvider != null) { Console.WriteLine(LogTextProvider(doc)); } } OnProcessed(e); } Notice how we happen to reuse the same event data for each event we raise. That’s safe to do because our event argument instance cannot be modified—its only property has a private setter. If it were possible for event handlers to change the event argument object, it would be risky to use the same one for both events. We could offer our colleagues on the production team another facility using these events. We already saw how they need to perform a quick check before each individual process to determine whether they should abort processing. We can take advantage of our Processing event to give them the option of canceling the whole process before it even gets off the ground. The framework defines a class called CancelEventArgs which adds a Boolean property called Cancel to the basic EventArgs . Subscribers can set the property to True , and the publisher is expected to abort the operation. Let’s add a new EventArgs class for that (see Example 5-28 ). Example 5-28. A cancelable event argument class class ProcessCancelEventArgs : CancelEventArgs { public ProcessCancelEventArgs(Document document) { Document = document; } public Document Document Notifying Clients with Events | 177
Image of page 201
{ get; private set; } } We’ll update the declaration of our Processing event, and its corresponding helper, as shown in Example 5-29 (but we’ll leave the Processed
Image of page 202

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 203
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern