Reduced dependencies. An ASP.NET page allows the programmer to implement methods within a page. As the Single ASP.NET Page shows, this can be useful for prototypes and small short-lived Web applications. As the complexity of the page, or the need to share code between pages, increases, it becomes more useful to separate portions of the code.
Reduced code duplication. The GetRecordings and GetTracks methods in the DatabaseGateway class can now be used by other pages. This eliminates the need to copy the methods into multiple views.
Separation of duties and concerns. The skill set for modifying the ASP.NET pages is different from the skill set for writing code that accesses the database. Separating the view and the model, as shown earlier, allows specialists in each area to work in parallel.
Optimizing opportunities. Separating the responsibilities into specific classes, as shown earlier, increases the opportunities for optimization. In the example described previously, the data is loaded from the database every time a request is made. It would be possible to cache the data in certain situations, which could improve the overall performance of the application. This, however, would be difficult or impossible without separating the code.
Testability. Isolating the model from the view makes it possible to test the model outside the ASP.NET environment.
Additional code and complexity. The example shown earlier adds more files and code, which increases the maintenance cost of the code when changes must be made to all three roles. In some cases, making the changes in one file is easier than separating out the changes into multiple files. The extra cost must be weighed against the reasons for separating the code. For small applications, the cost might not be justified.
For more information, see the following related patterns:
Table Data Gateway. This pattern is an object that acts as a gateway to a database table. One instance handles all the roles in a table. [Fowler03]
Bound Data Control. This pattern is a user interface component that is bound to a data source and can render itself on the screen or page.
[Fowler03] Fowler, Martin. Patterns of Enterprise Application Architecture. Addison-Wesley, 2003.