Design Patterns in world of Sharepoint – Part 1 – Model View Presenter

, , Leave a comment

I will be writing series of posts on design patterns which can be handful during SharePoint development.
To start with, this post is all about MVP – Model View Presenter design pattern.

I am sure you are already knowing about this design pattern but just as summary – MVP is a UI design pattern but what does that mean? As a layman go back to the basics, we started with 3 tier architecture
a) Presentation
b) Business Logic
c) Data Layer
MVP is nothing but a design pattern around Presentation layer which helps with extending and testing the UI layer.
[Model] — Presenter —- [View1][View2]…..
I will not go much in detail about MVP basics as this post is all about implementing MVP in SharePoint

In this example, I will create multiple web parts to get employees data from a list based on the Employee Type filter applied in the respective webpart using MVP pattern.

Below is screenshot of how much visual studio solution looks like. I will go through each class individually after this.

IEmployeesView

Nothing much here. Each view will implement IEmployeesView which has two properties EmpType and Employees. EmpType is used to filter the data from SharePoint list. Based on what this property is set to like “HR”,”Sales” etc… related Employees will be fetched and shown on the page.

Before going into views implementation, i will go through Model quickly


You see Model implementation has nothing but a static method for searching and getting data based on empType passed as argument to the method.

Now lets check View Implementation

SalesEmployees extends WebPart and Implements IEmployeesView. Like mentioned earlier IEmployeesView has two properties to implement i.e. EmpType and Employees. In SalesEmployees since we only need “Sales Employees” we are returning “Sales” for EmpType. Employees will set in Presenter Implementation. If you see createchildcontrol, there we are initializing EmployeePresenter and then method to search the data is called. Now lets check the presenter class


In Presenter class, we are setting the Employees property of the View passed on to the Presenter class implementation with data received from GetData method from Model implementation.

You can have multiple views implementation using the presenter to get and process the data like a view for “HR employees”, “All employees” etc.

Hope you like the post. Do leave your comments/feedback/inputs etc.

Thanks
Anirudh Gupta