Visual Studio Ultimate 2012: Developing, testing, and production debugging for SharePoint

Working with Sharepoint 2013 and VS2012, this is for you.

Webpart life cycle

Whenever I'm asked to describe the life cycle of SharePoint webpart,I always get mixed up with the sequence of events. Every time this happens I google and read about it, only the fumble again next time.

Time has come to settle this once and for all time, I looked around and found two nice graphics on this.



SharePoint Web Part - Event Flow chart



Implementation of MVP (Model View Presenter) design pattern in SharePoint webpart

MVP design pattern

The Model-View-Presenter pattern is an interactive application architecture pattern used to separate the concerns of an application’s data, presentation, and user input into specialized components.
  • The Model is an interface defining the data to be displayed or otherwise acted upon in the user interface.
  • The View is a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
  • The Presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.

Advantages

  1. Clean separation on concerns make it easier to understand and maintain code.
  2. MVP allows targeting different GUI platforms (Windows, Web, Silverlight, etc.) Thus the same application can be used with quite different presentation layers - one for Windows, the other for Silverlight or Web environment, etc.
  3. It makes it possible share code between pages that require the same behavior.
  4. It maximizes the code that can be tested with automation. (Views are hard to test.)

MVP in detail (references)

SharePoint Guidance by Microsoft Patterns and practices demonstrates implementation of MVP in context of SharePoint webparts. It is included in Reference Implementation: The Sandbox Execution Model.

Solution scenario


As shown in the above figure, we want to create a webpart, AggregateView Web Part which queries lists in various subsites of a site collection and displays  the aggregated data in an ASP.Net GridView control at the Root site of a site collection.

AggregateView Web Part implementation

Component Classes


The AggregateViewPresenter class represents the Presenter component in the MVP pattern. This class
performs the following tasks:
  1. It retrieves a DataTable from the model represented by the IEstimatesService.
  2. It sets the DataTable as the data source in the view represented by IAggregateView.

The AggregateView class represents the View component in the MVP pattern. This class is the actual Web Part.This class performs the following tasks:
  1. It instantiates the Presenter object represented by the AggregateViewPresenter. To do this, it creates an instance of the Model represented by EstimatesService, and then it constructs the AggregrateViewPresenter, passing in itself as the View and the EstimatesService as the Model.
  2. It renders the data supplied by the presenter to the UI.

Finally, the EstimatesService class represents the Model component in the MVP pattern. This class performs the following tasks:
  1. It executes a query to retrieve data from various lists on each subsite.
  2. It returns the data to the caller in a DataTable.
The following figure describes the flow of execution described above: