asp.net MVC involves 5 synchronous and asynchronous, are you stupid to know? [Next Article]

Source: Internet
Author: User
Tags abstract visual studio

About the five components involved in ASP.net MVC's handling of requests (synchronous or asynchronous), we talked about three in the previous article (Mvchandler, Controller and Actioninvoker), and now we're going to talk about the remaining two, That is, Controllerdescriptor and actiondescriptor, the implementation of these five components is not isolated, but has an agreed relationship. Trust the reader to read these two articles carefully, you will have a deep understanding of the way the whole request is handled.

Synchronization and Asynchrony of Controllerdescriptor

If the use of controlleractioninvoker,action is always synchronous, but when Asynccontrolleractioninvoker as controller Actioninvoker, Does not mean always executing all the action in an asynchronous fashion. As to how the two types of Actioninvoker specifically implement the action, it also involves two descriptive objects, The Controllerdescriptor and actiondescriptor that are used to describe controller and action.

By introducing the two objects in the previous "model binding", we know that there are two specific controllerdescriptor in the ASP.net MVC application Programming interface, That is, Reflectedcontrollerdescriptor and Reflectedasynccontrollerdescriptor, which represent the synchronous and asynchronous versions of Controllerdescriptor respectively.

   1:public class Reflectedcontrollerdescriptor:controllerdescriptor
2: {
3: //ellipsis member
4:}
5:
6:public class Reflectedasynccontrollerdescriptor:controllerdescriptor
7: {
8: //ellipsis member
9:}

Reflectedcontrollerdescriptor and Reflectedasynccontrollerdescriptor are not descriptions of controller that implement IController and Iaynccontroller interfaces, respectively. , but a description of the controller that directly inherits from the abstract class controller and Asynccontroller. The difference between them is the difference between the creator, By default Reflectedcontrollerdescriptor and Reflectedasynccontrollerdescriptor are passed Controlleractioninvoker and Asynccontrolleractioni respectively. Nvoker to create the. The relationship between Actioninvoker and Controllerdescriptor can be represented by UML as shown in the following illustration.

The relationship between Actioninvoker and Controllerdescriptor can be validated by a simple example. In an empty Web application created through the ASP.net MVC project template in Visual Studio, We have customized the following two types of actioninvoker that inherit from Controlleractioninvoker and Asynccontrolleractioninvoker respectively. In these two custom actioninvoker, the public Getcontrollerdescriptor method is defined to overwrite the method with the same name as the base class (the protected virtual method). and directly call the base class's method of the same name to the corresponding Controllerdescriptor object according to the provided controller context.

   1:public class Fooactioninvoker:controlleractioninvoker
2: {
3: Public new Controllerdescriptor getcontrollerdescriptor (ControllerContext controllercontext)
4: {
5: Return base. Getcontrollerdescriptor (ControllerContext);
6: }
7:}
8:
9:public class Baractioninvoker:asynccontrolleractioninvoker
10: {
One: public new Controllerdescriptor getcontrollerdescriptor (ControllerContext controllercontext)
: {
: Return base. Getcontrollerdescriptor (ControllerContext);
: }
15:}

We then defined two controller types, all of which are direct inheritors of the abstract type controller. As shown in the following code snippet, both controller classes (Foocontroller and Barcontroller) override the virtual method Createactioninvoker, The type of Actioninvoker returned is the Fooactioninvoker and baractioninvoker we defined above. In the default action method index, we use the current actioninvoker to get the Controllerdescriptor object that describes the controller and to present its type.

1:public class Foocontroller:controller
2: {
3:protected override Iactioninvoker Createactioninvoker ()
4: {
5:return new Fooactioninvoker ();
6:}
7:
8:public void Index ()
9: {
10:controllerdescriptor controllerdescriptor = (fooactioninvoker) this. Actioninvoker). Getcontrollerdescriptor (ControllerContext);
11:response.write (Controllerdescriptor.gettype (). FullName);
12:}
13:}
14:
15:public class Barcontroller:controller
16: {
17:protected override Iactioninvoker Createactioninvoker ()
18: {
19:return new Baractioninvoker ();
20:}
21st:
22:public void Index ()
23: {
24:controllerdescriptor controllerdescriptor = (baractioninvoker) this. Actioninvoker). Getcontrollerdescriptor (ControllerContext);
25:response.write (Controllerdescriptor.gettype (). FullName);
26:}
27:}

Related Article

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.