Community Server Topic V: ihttphandlerfactory

Source: Internet
Author: User
Server

If you understand factory design mode, this article can not see, as long as the configuration IHttpHandlerFactory can be, do not understand also does not matter, through the following article you can learn how to use IHttpHandlerFactory, It also understands the broad definition of factory design patterns

In the previous topic, I explained the application of HttpHandler in CS and some related topics. In fact, there is another option to implement the HttpHandler function is handler Factory (CS does not use handler Factory), which is achieved by inheriting the IHttpHandlerFactory interface.

The IHttpHandlerFactory interface is defined as follows:

Interface IHttpHandlerFactory
{
IHttpHandler GetHandler (HttpContext ctx, String RequestType, String url,string pathtranslated);
void Releasehandler (IHttpHandler handler);
}

2 methods are defined in this interface, and the GetHandler method is invoked at the start of the request, and Releasehandler is invoked when the request is finished and all handler are no longer needed.

How to use it? There are roughly three steps:

1: Define the class that actually handles the HttpHandler (implementing the IHttpHandler Interface), which is called in the Handlerfactory for business processing, A handlerfactory can call n a HttpHandler class (otherwise it's not called Factory!). ) to process the relevant business logic.

2: Define Handlerfactory, Example:

public class Myhandlerfactory:ihttphandlerfactory
{
Public IHttpHandler GetHandler (HttpContext ctx,string requesttype,string url,string)
{
\ \ Processing logic, and finally returns a HttpHandler class
}
public void Releasehandler (IHttpHandler handler) {}
}

The previous step said, a handlerfactory can deal with N HttpHandler, when to deal with what HttpHandler is here in the processing logic, such as: The above a topic in the chain of Anti-Theft said, You can define two IHttpHandler implementations to handle requests for pictures and compressed files, respectively. Because the HttpContext context is passed in in the GetHandler method, you can judge the URL request in the context and then enable the different HttpHandler classes.

3: Register this handlerfactory in the Web.config file, the way of registration and HttpHandler is the same. Cases:

<add verb= "*" Path= "*.*"
Type= "MyNamespace. Myhandlerfactory, myassembly "/>

Note that there is no more configuration for HttpHandler, no matter how many IHttpHandler instances you define, as long as they implement their functions through httphandlerfactory calls, do not register it here or repeat it.

IHttpHandlerFactory is actually very simple, the idea of existence is the factory design pattern (Factory mode: to pass different parameters to the factory object to return objects that have the same base class or implement the same interface. This means returning an object with the same IHttpHandler interface.

When you want to use IHttpHandlerFactory instead of using IHttpHandler, I'll make a summary of my personal development experience:

1: In a project need to use a lot of IHttpHandler, and these ihttphandler judgments are repeated to do a same prophase processing, if the anti-theft chain example of the picture and compression of two different HttpHandler processing, But they are consistent in their judgment of URLs, using ihttphandlerfactory instead of IHttpHandler in. Text, and all requests for URLs (except *.asmx, error.aspx) The turnover is treated uniformly by a ihttphandlerfactory inlet.

2: From deployment and loose coupling, if your Web application is big enough (I haven't designed this large app yet), You have to consider whether to change the Web.config configuration when changing the HttpHandler processing class, this change will restart the entire app, for some systems this is not to be done casually. At this time, the unification of a portal ihttphandlerfactory is very important, the portal is relatively unchanged, and IHttpHandler implementation can be used to plug the custom XML file to achieve loose coupling, using some reflection of what can be achieved.

If you have other ideas for ihttphandlerfactory, please leave your point of view.







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.