Refactor CAB Smart Part Quick Start Project to “Presentation Model”
Composite UI Application Block (CAB) is really a powerful framework for smart client applications. It comes with samples implementing the Model View Control (MVC) pattern and the Model View Presenter (MVP) pattern. There is actually a third pattern called Presentation Model. See the pictures from http://jgoodies.com/articles/patterns-and-binding.pdf.
As described in Martin Fowler’s article (MVP, PM), the presenter in MVP has no state and the views need to provide interface allow presenter to manipulate them. The Presentation Model holds and synchronizes the state, but no need to create view interfaces.
Choice between MVP and PM, as per Martin Fowler’s article:
Compare to MVC or MVP, I prefer using the Presentation Model, because it has a centralized place to store state/data and centralized event source to notify views to update themselves. The CAB’s Event Broker is perfect for notification. So I modified the Smart Part Quick Start Project to become the ”Presentation Model” pattern. Here is the Source code.
What have I done?
- Refactor to extract interface ICustomerListView and ICustomerDetailView from the views*.
- Added CustomerPresentationModel class that implements the above two interfaces.
- Define the CustomerPresentationModel class as Service. **
- Inject the CustomerPresentationModel instances into views using service dependency injection. ***
- Published events CustomerSelected events CustomerCommentsSelected from the CustomerPresentationModel class.
- Subscribed the above events in BrowseCustomerWorkItem and ViewCustomerWorkItem
* Extract interface from the views sound very much similar to MVP, isn’t it? The difference is that in MVP, the presenter uses the interfaces to push data into the views, means that presenter refers to views. In PM, the controller implements the interface. The views pull data from the controller, means that views refer to controller. The “refer to” is a very good case of using depedency injection.
**
public class CustomerPresentationModel: ICustomerDetailView, ICustomerListView
{
}
***
public partial class CustomerListView : TitledSmartPart
{
private ICustomerListView controller = null;
[ServiceDependency(Required = true, Type = typeof(CustomerPresentationModel))]
public ICustomerListView Controller
{
set { controller = value; }
}
….
}
Tags: smart client Composite UI Application Block MVC Model View Presenter Presentation Model injection
1 comment May 15th, 2006