How to Implement MVVMP Pattern
First I explained what MVVMP is and then I illustrated how it makes more sense than MVVM. Now I’m going to show you how to implement MVVMP a.k.a. Model View ViewModel Presenter pattern using PRISM and WPF.
I have created a stub project that I’ll explain just now and I have built a sample calculator application using that to demonstrate the use of pattern. Our Stub Prism application has following 3 projects
Shell project contains the Shell Window and the bootstrapper. I will not go into details of things that require knowledge of prism since the main purpose of this blog post is to explain MVVMP.
Our second project i.e. Infrastructure needs to have a few interfaces and base classes which are as follows:
- IView: Every view interface has to derive from this interface and it has a convinent DataContext property that allows us to set the datacontext for the views.
- IPresenter: Again every presenter has to implement this interface. It has a view property that we will use to return the view from presenter when it is resolved from container.
- ViewModelBase: This is the base class for all view models that has one important property called KeepAlive
- PresenterBase: This base class does the basic job of instantiating the view model and assigning the presenter instance to KeepAlive property.
The ViewModelBase needs to have KeepAlive propery because we need presenter to stay alive for as long as view is alive and there is nothing that is referencing the presenter.
View doesn’t get garbage collected because it is loaded in VisualTree
ViewModel doesn’t get garbage collected because it is DataContext of view and hence referenced by View
Presenter doesn’t get garbage collected because it is referenced by ViewModel using KeepAlive property
If we don’t have this property then presenter will get garbage collected after some time and your view will become dysfunctional.
So at this point we have all the essential ingredients we need to build our MVVMP based application.
The final step is to now have the set of view,presenter and viewmodel instantiated and stitched when the module is initialized so in the Initialize method of our module we register all the types against their respective interfaces.
The RegisterViewWithRegion call is as follows:
RegionRegistry.RegisterViewWithRegion(RegionNames.SampleRegion, () => Container.Resolve<IPresenter<ISampleView>>().View);
Notice that instead of specifying the ViewType we’re using the getContentDelegate overload which allows us to instantiate the presenter by resolving it using Unity and then we can return the public View property.
Here we are using Presenter first pattern i.e. we resolve the presenter and we is instantiated as a side effect (because it is a constructor dependency of presenter).
So thats it; when the shell is loaded, the presenter is automatically created, the resulting view is injected in shell and the presenter can now manipulate the ViewModel using its ViewModel property and View using the ViewProperty.
This pattern can also be used in a wpf application that doesn’t use prism.
It only depends on dependency injection mechanism therefore you need something like Unity to instantiate and inject the views in presenters or you can use some kind of factory class to do that for you (Poor man’s DI).
I have also created a sample wpf application that demonstrates MVVMP without using prism.
The projects are hosted at http://mvvmpsample.codeplex.com
Download the code and use it in your own project without any restrictions (MIT License).
Categorised as: Programming