tags:

views:

28

answers:

1

I'm debating with myself about the best practice approach to controlling an Aggreate View Model class that I've created for my app... Let's say I have an aggregate model that has a PurchaseOrder object and a list of line items that belong to that Purchase Order, and a few other auxuliary/related objects. This view model is just a wrapper around all these objects that you would typcially need when working on any give PurchaseOrder.

After after creating an instance of this view model, I then want it to load up a PurchaseOrder (and it will load the PurchaseOrderLineItems automatically and saturate all the other related objects)...

So, to instruct the view model to load up a PurchaseOrder, is it more acceptable to:

  1. Instruct the view model by setting a property on it (and let the property setter of the view model class respond by loading up the data)

    ViewModel.PoNo = 1234;

or

  1. Call a method on the view model to do the work:

    ViewModel.LoadPurchaseOrder(1234);

Just to give a few mode detials about the Aggregate View Model, it basically looks like this:

    public class ViewModel
    {
      //-- private fields
      PurchaseOrder _Po = new PurcaseOrder();
      List<PurchaseOrderItem> _PoLineItems;
      Vendor _Vend = new Vendor();
      int _PoNo;

      //-- public properties here

      ViewModel(){} // Constructor

    }
+1  A: 

Does this ViewModel serve any purpose other than relating all the PurchaseOrder informaton together? If not i would say you should pass your purchase order in the constructor of ViewModel because it seems like ViewModel would only be in a valid state if it had a PurchaseOrder.

EDIT: Given the 2 options you have listed, I think a method call makes more sense than setting a property as it is easier to tell that you are loading a PurchaseOrder into this ViewModel. As a developer, I wouldn't think that setting an integer property would end up loading all kinds of objects on the ViewModel, but one might expect that from calling a method.

Matt Dearing
Well, you could certainly extend the ViewModel class to accept a PurchaseOrder and react appropriately, but for this particular program flow, I do not pre-fetch the PurchaseOrder object or otherwise have access to it to pass it in... I want the ViewModel to handle that for me when I set a property or call a method on it.
MattSlay
It just sounds like ViewModel is dependent on having a PurchaseOrder so it doesn't make a lot of sense to create ViewModels who have no PurchaseOrder and therefore are unusable (since I would assume all the members on ViewModel have to do with a PurchaseOrder). Given the options you have listed I have updated my answer.
Matt Dearing
Very well. I like your comment that you "wouldn't think that setting an integer property would end up loading all kinds of objects on the ViewModel", but that you would expect that from a method call. That makes it very clear to me that method call is a more logical way to go.
MattSlay