concept Presenter in category sharePoint

appears as: Presenter, Presenter, The Presenter
SharePoint 2010 Web Parts in Action

This is an excerpt from Manning's book SharePoint 2010 Web Parts in Action.

The MVP pattern not only makes unit-testing easier but also facilitates code reuse. For instance, the same Model and Presenter can be used for different user interfaces, such as a Web Part, a page, or a Silverlight application. In addition, decoupling enables different developers to build different pieces, independently. That way, you can use the appropriate type of developer for the right task; user interface developers can build the views, and so on.

The basics of the MVP pattern is that the View is responsible for the user interface and is defined using an interface that acts as a contract for the Presenter. All actions in the user interface are forwarded to the Presenter, which is responsible for updating the View through the interface. The presenter uses the Model to access the domain, data, or underlying systems.

The Passive View variation of the MVP pattern is the cleanest version of the two. As you can see in figure 13.3, the View forwards user actions to the Presenter, which updates the View through the interface. The Presenter uses information from the Model to update the View. The Presenter optionally responds to events fired in the Model if the Model is updated by someone other than the Presenter. The View is totally unaware of the Model.

Figure 13.3. The Passive View design pattern divides the application into three layers; the Presenter stays in the middle updating the View with information and data from the data Model.

This decoupled pattern allows you to test the Presenter and Model independently. As always, the user interface is difficult to unit-test, but the Presenter can be replaced with a mock object that makes it easier to manually test the interface.

The first thing you’ll define after adding a new Web Part to your solution is the View interface. This interface will be the contract that the Presenter uses to update the View. The View interface, IListsView, is implemented as follows:

public interface IListsView {
    IEnumerable<ListInfo> Lists {
        get;
        set;
    }
}

The Lists property defined in the interface returns an enumerator with ListInfo objects. The ListInfo object is a class that contains the name (Title) of a list and its default view URL (Url). The reason that a custom class is used instead of the SharePoint SPList object is that this approach decouples the View and the Presenter from SharePoint and allows for easier testing. The ListInfo class is defined like this:

The Presenter is the glue that connects the data provided by the Model to the View. In the MVP pattern, the Presenter interacts with the View implementation through the interface. You create the Presenter like this:

public class ListsPresenter {
    protected readonly IListsView _view;

    public ListsPresenter(IListsView view) {
        _view = view;
    }
    public void LoadLists() {
        ListsService service = new ListsService();
        _view.Lists = service.GetLists();
    }
}

The Presenter contains a reference to the IListsView interface through a protected variable. The variable is set as readonly, which means that it’s immutable and can only be set in the constructor of the class. The constructor of the Presenter accepts the View interface to keep the reference. This method of injecting a dependency is called dependency injection; specifically this is constructor injection because you insert the dependencies in the constructor. The LoadLists method, which is the business logic, uses the service class (Model) to retrieve the lists and finally updates the View through the internal property.