Enterprise Solution Patterns Using Microsoft. NET 2003

Context

You have decided to use the Model-View-Controller (MVC) pattern to separate the user interface logic from the business logic of your dynamic Web application. You have reviewed the Page Controller pattern, but your page controller classes have complicated logic, are part of a deep inheritance hierarchy, or your application determines the navigation between pages dynamically based on configurable rules.

Problem

How do you best structure the controller for very complex Web applications so that you can achieve reuse and flexibility while avoiding code duplication?

Forces

The following are specific aspects of the forces from Model-View-Controller that apply to the Front Controller pattern.

The following forces might persuade you to use Front Controller as opposed to Page Controller:

Solution

Front Controller solves the decentralization problem present in Page Controller by channeling all requests through a single controller. The controller itself is usually implemented in two parts: a handler and a hierarchy of commands (see Figure 3.15).

Figure 3.15: Front Controller structure

The handler has two responsibilities:

Figure 3.16 shows these two responsibilities.

Figure 3.16: Front Controller, typical scenario

The commands themselves are also part of the controller. The commands represent the specific actions as described in the Command pattern [Gamma95]. Representing commands as individual objects allows the controller to interact with all commands in a generic way, as opposed to invoking specific methods on a common command class. After the command object completes the action, the command chooses which view to use to render the page.

Example

See Implementing Front Controller in ASP.NET Using HTTPHandler.

Resulting Context

Using the Front Controller pattern results in the following benefits and liabilities:

Benefits

Liabilities

Testing Considerations

Removing business logic from the views simplifies the testing of the views, because you can then test the views independent from the controller. As described in the Page Controller pattern, testing the controller may be hindered by the fact that the controller contains code that makes it dependent on the HTTP run-time environment. This dependency may be resolved by using a two-stage Web handler as described in Martin Fowler’s book, Patterns for Enterprise Application Architecture [Fowler03], and in the Page Controller pattern. The controller is separated into two parts: a Web handler and a dispatcher. The Web handler retrieves data from the Web request and passes it to the dispatcher in a way that the dispatcher does not depend on the Web server framework (for example, in a generic collection object). This allows for the dispatcher to be tested without the Web server framework being present.

Related Patterns

For more information, see the following related patterns:

Acknowledgments

[Alur01] Alur, Crupi, and Malks. Core J2EE Patterns: Best Practices and Design Strategies. Prentice Hall, 2001.

[Fowler03] Fowler, Martin. Patterns of Enterprise Application Architecture. Addison-Wesley, 2003.

[Gamma95] Gamma, Helm, Johnson, and Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

Категории