This project has moved and is read-only. For the latest updates, please go here.

complex SubViewModel?

Jul 3, 2011 at 9:51 AM

hey hi,

this framework is jus awesome. and it gives me more inspiration to learn lot new things. as a novice of MVVM and WAF I am getting more questions and trying solve most of the things on my own.

but now, I strucked up in a scenario of merging multiple view models. that is, I have five tables and they has FK relationships between them. for example person,address,status,purchaseorder,paymentmode. I would like to list all the purchaseorder with person Name, status and address. what is the right way to do this..

I appreciate any help on this.

Thanks,

Jul 9, 2011 at 6:37 PM

Hi,

Thanks for the feedback about WAF.

If I understand your question right then you are creating a ViewModel per database table. Maybe a better strategy is to create ViewModels per View which are defined on how you are going to show the data to the user. In this case you have a PurchaseOrderViewModel which exposes the PurchaseOrder, Person and Address Model (or DataModel) objects in a list so that you can bind from the PurchaseOrderView to this list.

Best Regards,
jbe

Jul 11, 2011 at 11:13 AM

Thanks for the reply jbe.

My scenario is something like this,

I should display the purchaseorderlist in three different grid based on the status.

first grid should display all the P.O which are "inprogress", other two must display the P.O with the status "completed", "pending" respectively.

what I have done is, I have declared three IEnumerable<PurchaseOrder> variables in Viewmodel, and in the constructor of ViewModel, I have extracted the  relevant Purchase orders based on its status from the purchaseorder ObservableCollection (using linq) and stored them in the variables. So I can bind this IEnumerable variables in the View (Grids).

I would like to know, whether am in the right path of handling my scenario ? if so, whenever I change something or delete a particular record,  I have to redo the same work in the controller (extracting the P.O based on its status) and rebind the grid in the View.

any suggestions / guidelines ??

Jul 11, 2011 at 9:58 PM

This sounds good to me. I believe you are on the right path.

Here a few comments:

You write that the constructor of the ViewModel is responsible for the linq queries. But a Controller is responsible to update the P.O when a record changes. Personally, I would let the Controller be responsible for both. Let the ViewModel provide only simple properties with the IEnumerable results from the queries which are set by the Controller.

The “rebind the grid in the View” happens automatically when the ViewModel properties call the RaisePropertyChanged method.

Jul 12, 2011 at 12:11 PM

I agree with the approach advised by jbe but share your concerns that you will need to rebuild the lists each time something changes. To avoid this I tend to wrap it up using a FilteredObservableCollection:

In my contoller:

var viewModel = new PurchaseOrderViewModel();
viewModel.InProgressPurchaseOrders = new FilteredObservableCollection(entityService.PurchaseOrders, x => x.Status == "inprogress");
viewModel.CompletedPurchaseOrders = new FilteredObservableCollection(entityService.PurchaseOrders, x => x.Status == "completed");
viewModel.PendingPurchaseOrders = new FilteredObservableCollection(entityService.PurchaseOrders, x => x.Status == "pending");


Since entityService.PurchaseOrders is observable, any changes (such as adding or deleting a PO) will be automatically reflected in the filtered collections and you don't need to worry about a thing.

The implementation of FilteredObservableCollection is a little difficult. My own implementation would not work for your case since you would also need it to observe property changes on the collection items (e.g. when PurchaseOrder.Status is changed). However, it is possible and it would be tidy, encapsulated and reusable. I use it all the time in MVVM/WAF. 

Jul 12, 2011 at 4:01 PM

Thanks jbe. Thanks skina.

yes. I changed my Filtering stuff in the controller itself but still the rebind of the grid was needed since we are not binding the grid with the actual observablecollection.

skina, your trick worked on this.. I have followed the way which shown here: http://info.titodotnet.com/2011/03/observablecollection-filtering.html

it works like a charm !!

Thanks a ton !!