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:
-
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
-
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
}
Does this
ViewModelserve any purpose other than relating all thePurchaseOrderinformaton together? If not i would say you should pass your purchase order in the constructor ofViewModelbecause it seems likeViewModelwould only be in a valid state if it had aPurchaseOrder.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.