PETR
programming c# 4.0.pdf

So instead of declaring our own delegate delegate

Info icon This preview shows pages 180–183. Sign up to view the full content.

So, instead of declaring our own delegate: delegate void DocumentProcess( Document doc ); we could just use an Action<> like this: Action<Document> A quick warning: although these are functionally equivalent, you cannot use an Action<Document> polymorphically as a DocumentProcess —they are, of course, different classes under the covers. We’re choosing between an implementation that uses a type we’re de- claring ourselves, or one supplied by the framework. Although there are sometimes good reasons for going your own way, it is usually best to take advantage of library code if it is an exact match for your requirement. So, we can delete our own delegate definition, and update our DocumentProcessor to use an Action<Document> instead, as shown in Example 5-12 . 156 | Chapter 5: Composability and Extensibility with Delegates
Image of page 180

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

Example 5-12. Modifying the processor to use the built-in Action<T> delegate type class DocumentProcessor { private readonly List< Action<Document> > processes = new List< Action<Document> >(); public List< Action<Document> > Processes { get { return processes; } } public void Process(Document doc) { foreach (Action<Document> process in Processes) { process(doc); } } } Compile and run, and you’ll see that we still get our expected output. If you were watching the IntelliSense as you were typing in that code, you will have noticed that there are several Action<> types in the framework: Action<T> , Action<T1,T2> , Action<T1,T2,T3> , and so on. As you might expect, these allow you to define delegates to methods which return void , but which take two, three, or more parameters. .NET 4 provides Action<> delegate types going all the way up to 16 pa- rameters. (Previous versions stopped at four.) OK, let’s suppose that everything we’ve built so far has been deployed to the integration test environment, and the production folks have come back with a new requirement. Sometimes they configure a processing sequence that fails against a particular docu- ment—and it invariably seems to happen three hours into one of their more complex processes. They have some code which would let them do a quick check for some of their more compute-intensive processes and establish whether they are likely to fail. They want to know if we can implement this for them somehow. One way we might be able to do this is to provide a means of supplying an optional “check” function corresponding to each “action” function. We could then iterate all of the check functions first (they are supposed to be quick), and look at their return values. If any fail, we can give up (see Figure 5-3 ). We could implement that by rewriting our DocumentProcessor as shown in Exam- ple 5-13 . Generic Actions with Action<T> | 157
Image of page 181
Example 5-13. Adding quick checking to the document processor class DocumentProcessor { class ActionCheckPair { public Action<Document> Action { get; set; } public Check QuickCheck { get; set; } } private readonly List<ActionCheckPair> processes = new List<ActionCheckPair>(); public void AddProcess(Action<Document> action) { AddProcess(action, null); } public void AddProcess(Action<Document> action, Check quickCheck) { processes.Add(
Image of page 182

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

Image of page 183
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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