Introduction
The advantages of knowing Model-View-Presenter (MVP) pattern from scratch:
- Engineer's knowledge can be applied into SS different OOP programming ages (like VB. NET, Ruby, Java, C #). This is advantageous for developers and other ts working on different types of different ages.
- Engineer's knowledge can be applied against SS different versions of IDE and frameworks.
- Engineer becomes versatile and mobile Alibaba SS different platforms and ages.
- Engineer will not be dependent to MVP tools.
MVP tools are not bad, the downside of using tools is that they are often tied up to ide versions and often does not support older versions. tools have limited support on ages and frameworks, so if the engineer is given a project that does not support his beloved tool he'll go back to square one or worst he will blame the tool or start cursing the client/boss who gave project... Hehehe...
The goals of MVP are:
- Testability of the application
- Cohesion or separation of responsibility
- Orthogonality
OrthogonalityIs the ability to change the conceptual design of the application with minimal impact to the entire application. domino effect is common to two-tier application where a slight change to data access layer affects the presentation and the business process.
Advantages of MVP:
- Parallel Development. Separation of responsibilities of UI developer, business logic developer, and data access developer that make them work independently. in economics, it's division of labor.
- Reusability. Reuse presenter in different user interface designs (Web, Windows form, etc). I even use it in Visual Studio Tools for office (vsto) application.
- Maintainability due to clear separation of responsibilities (ui, application behavior, and domain ).
- User Interface independent. This is an advantage for an application that supports different user interface types.
Let's get it on!
Download the code and launch your Visual Studio. NET and open the mvpsession. sln file.
Note:The sample is built using Visual Studio 2005 and compatible with vs 2008. if you are running Visual Studio 2003 just change the ilist <t> types to ilist. I already embedded Ded the Assemblies we need for mocking (nmock) and unit testing (nunit ).
To initialize strate the advantages I mentioned above, I created a sample application that will insert new employee profile. this will show how MVP will be reused and consumed by two different user interface types with minimal impact or without impact at all to the presentation logic. it will also shows how to separate the presentation from the presentation logic.
A Tester Module is also added to show how to create a mock tester. mock testing is also known as horizontal testing whose main goal is to test the behavior of the application without going deeply to the business logic and data access layer. we will discuss more about mocking in MVP Part II.
Figure 1 shows the architecture reference of the sample application-user interface layer (presentation and presentation logic), business logic, and the data access layer. in this article we will just focus only on the user interface layer and how to test it.
Figure 1. Sample Application Architecture
PresentationIs the layer where the user interacts with the application and can be represented in your types of interfaces-mobile, web, Windows Forms, etc. in our sample application, we only use Windows form and web form.
Presentation logicHouses the behavior or the UI logic of the view. in most cases, it requires at least a view and a model in order for the presenter to work. service layer is sometimes optional if you don't have data to pull from data sources (Database, file, or Web Service ). I sometimes call this the MVP layer.
Parts of the presentation logic:
- ViewIs best described as the logical representation of the UI and provides an interface for the presenter to communicate with the UI.
- PresenterHouses the behavior and UI logic.
- ModelIs just an object that represents the module or the view. it's an ideal place to put the validation and small business rules like computation of total amount or formulas. this just a lump of data that holds the state and data of the view. in the sample application, our model is the employeemodel object. in an ordering system, the model wocould be the order object that contains order details and order items.
- ServiceIs a data provider and a fa ç Ade that manages data retrieval from different data sources (Database, Web Service, XML, etc ). it can also house some business logic and validation. it is not aware of the user interface type (whether it is a web, mobile, or Windows form ). in the sample application, our service layer only returns dummy data. if you have time, you can play with it by adding the BLL and/or dal layer.
Side note:
As a rule of thumb, do not include any UI libraries in your presentation or MVP project like system. web or system. windows. forms to avoid UI objects from mixing with the presenter code. this will make your presenter less flexible.
You can see that model has no direct access to service and only the presenter has direct access to both. but it does not mean that model cannot access the service. in most of my application I do not give model access to service as much as possible to centralize all the behavior and logic in the presenter and make model just a lump data. this makes the application easy to debug and test.
To start the implementation, let's assume that we have four engineers-Leo, Richard, Ryan, and Chris. leo is a Windows form UI Designer, Richard is a UI/presentation developer, Ryan is the backend developer, and Chris is the same ect.
Chris was given a project to create a data entry application for HR. chris decided to design the application in MVP pattern for agile development and to let engineer work in parallel. chris divided the project into three parts the UI interface layer-business logic layer-data access layer and distributed the task to the four engineers.
Leo as a UI Designer doesn' t care about how the data will be processed or how the form will behave when submit button is clicked. he only cares about how the data will be presented to the user and how to make the application user friendly to the users. so he created the form in Figure 2.
Figure 2. Windows Forms UI.
Richard as a presentation developer doesn't care about what control to use in each fields, what color of the error message and labels are, and what the UI type is, whether it is a Windows Forms or a Web form. he only cares about the fields needed and its data type, what are the required fields, validation rules, business flow, and the service provider. so he created iemployeeview, iemployee and employeemodel, employeeservice, and employeepresenter objects. see MVP Project for the implementation.
Ryan as a backend developer doesn't care about the presentation. he only cares about how to provide the client or consumer with the data, where to fetch the data, how to connect to the database, and how to ensure the integrity and consistency of data. in our sample application we don't have the BLL and Dal project yet, so just assume that Ryan is still working on it or probably sick. hehehe... we hope Ryan is finished with the dal and/or BLL by the time MVP Part II article is ready.
Question:Do Richard requires to integrate his work to the UI and backend code before he can test the behavior and flow?
Answer:No. richard cocould proceed testing his work without the UI and backend. this method of testing is called mock testing or horizontal testing. while Leo and Ryan are busy working on the UI and backend, Richard can proceed creating a mock tester project to test his work. see test project in the sample solution. in this sample we are using nmock for mock testing.
Late in the game, Marvin the project manager decided to add a web form UI that will be used by the global HR in the main headquarters. so he added Jen as a resource to design HTML forms. jen created the form in figure 3.
Figure 3.Web form
Question:Are we going to create a new presenter for the new web form UI?
Answer:No, we can reuse existing presentation with minor changes. Just take note that web is stateless so we need to modify interfaces that are statefull like the dialog boxes to Display error messages.
Three months have pasts. marvin comes in and approaches the team and says "guys we did a great job, the project was so great that attracts another client. he likes the system and just wants to change the salary range dropdown to list box and wants to connect to his company database through web service. "Chris the effecect evaluated and says" all we need to do is to modify the service and the form ". so the tasks are assigned to Leo and Richard.
Leo will just change comboxsalary to listboxsalary in the form.
Richard will just create a new service class that inherits the iemployeeservice and change the service used by the presenter in the form_load with the new service object named employeewebservice.
See winui2 project for the fixes made by Leo and Richard. in employeewebservice, I just provide a dummy data for the application to run. you can play with it with real Web service if you have time.
How to integrate presentation to UI
Integrating presentation to UI is just three easy steps away:
1. inherit the View Interface to UI. In our example its iemployeeview interface.
Public partial class formemployee: form, iemployeeview
{...
}
2. implement all the iemployeeview interfaces by providing code to each of the interfaces. See formemployee.
3. initialize the presenter in form_load.
_ Presenter. initialize ();
How to run the test project
There are several ways and tools to run the test project.
Running Test Project Using resharper (http://www.jetbrains.com/resharper)
If you have resharper you can just simply click the bubble besides the test case and select the run from tester context menu. This will launch the unit test runner within your ide.
Figure 4.0.Running Test Using resharper.
Fig 4.1. Unit test runner.
Running Test Project Using nunit (http://www.nunit.org /)
1. Run the nunit GUI.
2. Drag and Drop Test. dll found in test project's bin to nunit GUI. You can also use the File | open project, and then browse the test. dll.
3. Then click Run button.
Figure 4.3.Nunit Gui
Running Test Project Using testdriven. Net (http://www.testdriven.net /)
If you have testdriven. net installed you can just right click the test project, then select the test with the context menu and select one of the variety of test tools supported. if you select nunit it will launch nunit GUI.
For a quick test you can just select the run test (s) from the context menu.
Fig 4.4. Testdriven. Net context menu
The cases above shows
- Division of labor
- Productibetween and Efficiency
- Maintainability of the application
- Orthogonality
- Testability
There are times when you are developing your MVP application you'll encounter a sort of dead end where you develop a presentation that it's hard to test. if you are in this situation just refactor your code and remember the law of cohesion. sometimes your code gets tangled because of lack of cohesion.
May the Force be with you.
Rod