Using polymorphism, we can implement AOP (Aspect programming) and ashxaop in general processing programs (ashx ).

Source: Internet
Author: User

Using polymorphism, we can implement AOP (Aspect programming) and ashxaop in general processing programs (ashx ).

This article is a process of thinking about various solutions for code optimization (improved AOP aspect programming for login verification) for projects at work.

Project Background: A simple B/S questionnaire system built by ashx + nvelocity needs to optimize the logon verification process (it is a painful process to review the code after several months ~)

Nvelocity is a version of the velocity framework targeting. net. The core is to splice html strings and return the client. It is similar to the front-end and back-end code isolation of MVC. In addition, the general processing program ashx does not need to generate the control tree like asp.net, which saves time and effort in execution. Therefore, it is recommended that the simple system use ashx + nvelocity to build the system. If you care about the access address (for example, www.abc.com/news/index.ashx? The ashx Suffix in id = 234) is not easy to understand. You can use the module (HttpModule) to rewrite the url.

This article discusses how to embody the AOP aspect programming idea in ashx?

(1) review asp.net. All pages inherit from the Page class and can be implemented through the Page subclass. It turns out to be: Default: Page. After the section is inserted, it is: Default: LoginCheckPage, and LoginCheckPage: Page. In this way, you can write the login verification code in the LoginCheckPage class, and implement effective decoupling of all pages to be verified-decoupling is relative to writing a LoginCheck class, perform verification on each Default page (such as LoginCheck. check (context) is more conducive to modification and expansion.

(2) looking back at MVC, we can draw a gourd like asp.net above. It turns out to be: HomeController: Controller. After inserting the plane, it will be: HomeController: LoginCheckController, and LoginCheckController: Controller. In addition, you can use the feature labels on the class/method header for AOP.

There are still many practices in the implementation of AOP in asp.net and MVC. Here, we only want to compare them with the AOP of ashx: Can the above methods work in ashx? Yes! But make some minor adjustments:

(Write a LoginCheck class, and then add LoginCheck. Check (context) to each ashx. pr () to be verified. This is not a long term, so I will not talk about it here)

First try:(That's right. This is the last attempt in this article. However, the attempt process is to record your detours and hope to give readers more tips, thank you for your three attempts .)

Use HttpModule. Like url rewriting, isn't url rewriting processed every time a request comes, so the Module should also be able to log on-the difference between the two: normal url rewriting does not involve client isolation and does not take into account the requested resources. For login authentication, client isolation (cookie) is required, and the requested resources must be considered (not all resources are not accessed, some are tourists ).

For differences in request resources, you can overwrite them in the Code (which can be optimized to webcofig, other configuration files, and database storage) differential processing-match the request address with a regular expression to isolate the requests that require login verification from those that do not require authentication.

Can sessions be used directly in the Module for client isolation? First, make the HttpModule inherit from the IReadonlySessionState/IRequiresSessionState interface (HttpHandler is also the case), so that the Module can be recognized by. net when going through the pipeline and you want to use the session. Register the BeginRequest event. Do not forget to register webconfig. Everything is ready, debugging, and error -- the Session attribute in HttpApplication reports an error and the object reference is not set to the object instance. Is the registration event incorrect? I checked 19 events in the HttpApplication pipeline. The best starting point is between 10-11 events, that is, + = PostAcquireRequest, in order to perform login verification after obtaining the Session and before executing ashx.

However, there is no use of X ...... The object reference is still not set to the object instance. Why not? It's strange.

The Module is an application-level task, a filtering function, and a Session is a page-level task, it is necessary to perform different processing based on the sent request, so using Session in the Module is not the best solution. Therefore, the Module detour is abandoned.

Second Attempt:

Custom inherited from the ashx of IHttpHandler. Original: Index: IHttpHandler. After optimization: Index: LoginCheckHandler, LoginCheckHandler: IHttpHandler. The method used to insert the above asp.net and mvc to the inheritance tree. However, the debugging result does not go through the ProcessRequest () of the Index at all. The result is returned after the LoginCheckHandler. ProcessRequest () operation, and the client is blank. The reason is that the general IHttpHandler Processing Program (whether it is Index or LoginCheckHandler) will only execute ProcessRequest () once ().

Third attempt:

On the basis of the second method, change ProcessRequest () in LoginCheckHandler to virtual, override in the Index subclass, and call base. ProcessRequest (context) in Index. ProcessRequest ). During execution, the program ignores the PR method of the parent class because of the override, and the base in the Index subclass. PR () also requires the program to first take the PR method of the parent class, and combined with Response. the immediate output feature of Redirect () (first Flush, at End) can block requests that do not meet the logon authentication conditions. Success!

The trouble is that you need to change the subclass to override and the base exists in the subclass. PR () code (also, it is more crude than simply calling LoginCheck. check (context) to verify that the coupling degree is reduced.) Is there a better AOP method? I hope you guys can take a look at the official comments.

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.