Entry: Front Controller Pattern Friday, March 26, 2004



The Page Controller pattern becomes inefficient when you need to coordinate processing across multiple Web pages because of its implementation of a single object per logical page. The Front Controller is more efficient in such cases because it funnels all requests through a single controller and then directs requests through a single handler and a hierarchy of command classes. The handler retrieves parameters from the HTTP request, chooses the correct command, and transfers processing to it. After each command object performs the specified action, it can choose which view is required to render the page properly. Implementing the Front Controller results in more centralized application control because all page requests come through a single controller instead of being handled by different Page Controllers. But this can also be a liability if the handler does expensive processing, such as database lookups that could cause the entire application to operate slowly. The handler should be as efficient as possible and use external resources only when absolutely necessary. You should also consider caching any external resources to increase the handlerfs performance.

You implement the FrontController class by creating a Handler and a CommandFactory, which determines the necessary command to execute in response to a request. ASP.NET provides the IHttpHandler interface to allow developers to create custom interfaces required to service incoming HTTP requests. You implement the Handler by inheriting from System.Web.IHttpHandler and adding the logic to instantiate and call the appropriate command from the CommandFactory. The CommandFactory defines a collection of commands and the logic that determines which of the commands should be executed. Calling the CommandFactory returns the appropriate Command object for which the Handler can call an Execute method. Using this pattern, you can create more robust navigation scenarios and implement them centrally by extending the CommandFactory logic and creating additional commands to handle the required scenarios.

   0 comments

Leave a Comment:

Name


Homepage (optional)


Comments