This project is read-only.

M-V-VM Guidance for "Lookup feature" in ViewModel

Developer
Nov 14, 2010 at 11:22 AM

I've been meaning to ask about this for a while now and I hope there are other developers that have pondered about this also. It's easiest for me to conjure up an example to help explain what I mean.

Business Layer

Say you have a Customer business object (in CSLA terms an Editable Root) that has the following properties:

int Id {get; set;}
string Name {get; set;}
int StatusId {get; set;}

You also have a list of statuses defined in a StatusInfo object that looks something like:

int Id {get;}
string Description {get;}

There's StatusInfoList object that is a collection of StatusInfo objects (in CSLA terms the collection is a StatusInfoList : ReadOnlyList and the item is a StatusInfo : ReadOnlyBase).

User Interface Layer:

The required functionality is to change the status of a customer and save the customer - aka. the classic "lookup feature". From a pure code perspective it's something along these lines:

var statusList = BusinessLogic.StatusInfoList.Get(); // Fetch list of statuses
var statusSuspend = statusList.SelectSingle( s => s.Id == 2 ); // Get the status with Id value of 2 aka. Suspended

var customer = BusinessLogic.Customer.Get( 123 ); // Fetch customer with Id 123 from database
customer.StatusId = statusSuspend.Id; // Suspend the customer
customer.Save(); // Persist the status change

Using Windows Forms or Web Forms or even non MVVM Silverlight this is a simple scenario: Use a combobox / drop down list that get's its items from the statusList and using binding you set the combobox's selected value to the Customer's StatusId field.

Move to Silverlight with M-V-VM.

You have a ViewModel called CustomerViewModel that uses the Customer business object for the Model.
You have a View called CustomerView that as per BXF / CSLA samples uses a CollectionViewSource that maps to the CustomerViewModel.

(Maybe you can already see where I'm going with this).

On the CustomerView I would like to have a ComboBox where I can select the status value to bind to the customer view model property StatusId. That's easy.

My question is: Where is the most appropriate place for the data (i.e. StatusList) which will be bound to the items source property of the ComboBox?

I have done it in 3 different ways and I'm not sure which is best:

  1. On the View - Initial applications mixed in the CslaDataProvider for just those items - kinda before BXF's days (not very M-V-VM I know).
  2. On the ViewModel - I add dependency properties to the CustomerViewModel for the each "lookup list" e.g. the list of statuses.
  3. On a seperate ViewModel - I could create a separate ViewModel for those "lookup lists" and have the view bind to multiple ViewModels.

Option 1 is a pass for me - oldschool and not MVVM.

Option 2 works well, but feels combersome because I need to initialise the dependency properties before the model is "refreshed".
As an issue aside, I cannot get to populate multiple lookup lists concurrently because the Async calls (via the Silverlight DataPortal Client) puts these calls one after the other (PS: I'm using CSLA 3.8.4 still).

Option 3 on the face of it is nicely structured, with some separation of concern, BUT I don't know if this is appropriate for M-V-VM and the current BXF technique for loading views and populating them with ViewModels doesn't suit this scenario too well.

On this note, I came accross a nice ViewModelLocator implementation that is super dynamic and simple. Refer: http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/ . This could be something to consider for BXF.

What are your experiences for implementing a simple "lookup feature" with MVVM, with specific reference to the source for the lookup data?

I would really value and appreciate any inputs.

Regards,
Jaans

Developer
Nov 15, 2010 at 10:44 AM

Similar discussion over at CSLA Forums with some valuable insight and a workable solution

http://forums.lhotka.net/forums/t/9756.aspx