It can also be very difficult to configure the accumulators with delegation.

Source: Internet
Author: User

We know that in ASP. net mvc, when the ActionFilter method is applied to the Action method, its execution process is as follows:

, Kairong North nose...

The functionality of ASP. net mvc involves too much code, which looks messy. The following uses an example to introduce this code!

The example environment is as follows:

1. There is such an interface IFilterFunction, which defines two methods!

    public interface IFilterFunction    {        void Before();        void After();    }

2. One method!

Public string ActionMethed () {Console. WriteLine ("Action Method Content"); return "Action ";}

Demand comesTo perform operations Before and after the ActionMethod method, you must first execute all Before methods of all classes that implement the IFilterFunction interface, and then execute the ActionMethed method, then, execute the After method for all classes that implement the IFilterFunction interface! (The purpose is to perform some operations before and after the ActionMethod method is executed)

For such a requirement, the first thought can be implemented as follows:

Small hanging implementation:

Class for implementing the IFilterFunction Interface
public string Start(){FilterOne filterOne = new FilterOne();FilterTwo filterTwo = new FilterTwo();FilterThree filterThree = new FilterThree();filterThree.Before();filterTwo.Before();filterOne.Before();string str = ActionMethed();filterOne.After();filterTwo.After();filterThree.After();return str;}

Although it can be achieved, it is a little too pediatric to do so. What if I say yes?
Come on ......

Implementation of large cranes:

Class that implements the IFilterFunction Interface
Public string Start () {Func <string> continuation = () => ActionMethed (); List <IFilterFunction> lists = new List <IFilterFunction> (); lists. add (new FilterOne (); lists. add (new FilterTwo (); lists. add (new FilterThree (); // It is its Func <string> p = lists. aggregate (continuation, (newcontinuation, filter) => () => Do (newcontinuation, filter); return p ();} public string Do (Func <string> continuation, IFilterFunction filter) {filter. before (); string str = continuation (); filter. after (); return str ;}

The execution result is:

 

 

 

 

 


That's right, it's so awesome. You can use the accumulators to perform a layer-by-layer packaging delegate, then execute the delegate, and then execute it layer-by-layer. Its Process

ASP. net mvc uses this method to execute the Action method and ActionFilter. That is, use the accumulators to package the delegate!

FriendCan you write such code? Download instance source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.