I am very fond of using patterns when developing and I have seen the importance of patterns in several projects. When it comes to Windows Phone 7 development I guess the most popular pattern to use is the Model-View-ViewModel (MVVM) pattern. I have successfully used a different (but still quite similar) pattern for several mobile app projects. In this post I will show you how to use the Model-View-Presenter (MVP) pattern when developing Windows Phone 7 applications.
I will not go into details describing the MVP-pattern since I covered that pretty well in another post I wrote a while back: Using Model-View-Presenter (MVP) pattern in Compact Framework. What I will do is to show an example on how you can design your Windows Phone 7 app architecture to use MVP-pattern. I will use the same login example as I used in the Compact Framework post. You can see that the biggest difference between how you do this for CF and WP7 is the data binding capability in WP7.
Before we go to the code a short summary on the essence of the MVP-pattern: The View is only responsible for displaying output to the user and receiving input from the user. The View will always ask the Presenter what to do with the data and the Presenter will pass the data on to the View. The Model consists of business objects and can be used both by the Presenter and the View. The figure below explains the MVP-pattern.
Now it’s time to have a look at the code. For this example I have created a solution called MvpDemo. To this solution I have added two projects. One Silverlight for Windows Phone – Windows Phone Application project called MvpDemo and one Silverlight for Windows Phone – Windows Phone Class Library called MvpDemo.MVP.