Avoiding Model-View-Presenter with Web Client Software Factory

Avoiding Model-View-Presenter with Web Client Software Factory

by David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory

 

I talked about the Web Client Software Factory and how it greatly assists you with implementing the Model-View-Presenter Pattern in your web applications by automatically generating your presenter and view interface classes as well as injecting their dependencies:

Web Client Software Factory - Model-View-Presenter and Page Workflow - Examples and Tutorials

Using the Model-View-Presenter Pattern in your web applications is definitely a proven practice and highly recommended when you want to loosely-couple your web pages from the application as well as implement testing of your UI. However, there will be cases when using Model-View-Presenter may not be right for your application but you still want to use the Web Client Software Factory to build your web applications.

 

Model-View-Presenter is Optional

Model-View-Presenter is optional in web applications built with the Web Client Software Factory. The Patterns and Practices Team is assisting you with that proven practice ( via a wizard ) if you wish to use it, but is by no means forcing it down your throat.

If you look at the web page ( view ) created when you use the add view ( with presenter ) option to create your web pages, there are only a couple of items being added to the web page class to support Model-View-Presenter: 1) an interface that the page (view) implements, and 2) a property by which the presenter class is injected into the page.

 

public partial class AddCustomer : Page, IAddCustomer

[CreateNew]
public AddCustomerPresenter Presenter
{
    set
    {
        this._presenter = value;
        this._presenter.View = this;
    }
}

 

If you would prefer not to deal with the Model-View-Presenter Pattern, just remove the interface specification and replace the injection of the presenter class with the injection of the controller class:

 

public partial class AddCustomer : Page

private CustomerController _controller;

[CreateNew]
public CustomerController Controller
{
    set
    {
        this._controller = value;
    }
}

 

With this change, the Composite Web Application Block will now inject the CustomerController into the page, allowing us to eliminate the use of the Model-View-Presenter Pattern for the page.

Of course, if you plan never to use the Model-View-Presenter Pattern in your application, don't use the add view ( with presenter ) wizard. Add your web pages as you would normally and add the injection of the proper controller class via a property as mentioned above. This way you don't have to delete the interface class and presenter class that automatically get added to the business module each time you run the wizard.

 

Conclusion

When Model-View-Presenter is not appropriate for your application, you can still use the Web Client Software Factory to assist in building your web applications. It is still important to learn and understand when to use the Model-View-Presenter, however, so don't take this tutorial as a way to avoid the pattern - merely a way to leverage the Web Client Software Factory when the Model-View-Presenter Pattern is not “appropriate” for the application.

Source: David Hayden ( Microsoft MVP C# )

Filed: Web Client Software Factory

posted on Sunday, February 18, 2007 12:20 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices