Archive for May 15th, 2006

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.

MVC MVP PM

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:

This decision would be strongly influenced by a decent framework that supports such synchronization. If you have such a thing then Presentation Model seems the better choice.

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?

  1. Refactor to extract interface ICustomerListView and ICustomerDetailView from the views*. 
  2. Added CustomerPresentationModel class that implements the above two interfaces. 
  3. Define the CustomerPresentationModel class as Service. **
  4. Inject the CustomerPresentationModel instances into views using service dependency injection. ***
  5. Published events CustomerSelected events CustomerCommentsSelected from the CustomerPresentationModel class.
  6. 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.

**

[Service]
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:

1 comment May 15th, 2006


Calendar

May 2006
M T W T F S S
« Apr   Jun »
1234567
891011121314
15161718192021
22232425262728
293031  

Posts by Month

Posts by Category


1