MVVM and the proper place to customize data to fit a particular View

MVVM and the proper place to customize data to fit a particular View

Old forum URL: forums.lhotka.net/forums/t/10846.aspx


TSF posted on Friday, November 04, 2011

If I have a NVL object exposed to my view through a VM, and I want to add an extra item to the NVL that says "Make a selection" so that item can be the default in a combobox, should I add that extra item in the BO itself, or should I do it in my VM? I tried to add the extra item in the VM, but because the NVL is exposed as a property in a parent ReadOnlyBase UoW object I can't add items in the VM without an insert error. (Note - I'm following the ProjectGetter example in Rocky's e-book.)

If I follow the idea that objects should be created with a specific use-case in mind, then I guess I should be okay with adding this extra item in my NVL's DP Fetch method.  But for some reason that doesn't seem ideal.  For example, what if the UI designer wanted to do something different to indicate to the user that he has to pick a value from the list?

StefanCop replied on Friday, November 04, 2011

TSF
For example, what if the UI designer wanted to do something different to indicate to the user that he has to pick a value from the list?

I would treat this in the same manner as you would if the UI designer want to change labels: through localization. You also have a similar problem if he wants to change the text of validation messages or the friendly name of properties. To me it's good practice to put all texts, which an end user sees to ressources.

If your domain knowledge says, that the BO's property, which is bound to the combo box, should have a null value (undefined, not selected,...), the I would add it to your business NVL either right in the database or while the DP Fetch method.
Otherwise, I think it's more a presentation thing you want to solve, and in this case I wouldn't add in the business layer. This value doesn't intrinsically belong to your business. 

To overcome the read only restriction of your list, i.e. you can
  a) code an "add" method in your BO, that first sets the IsReadOnly temp. to false (as in DP Fetch),  if you look at the NVL as a presenation object
  b) create a simple .NET list for your presentation purpose and add there your item.

I usually choose b) because it often depends on the use case whether the user can select "nothing".

 

 

TSF replied on Friday, November 04, 2011

What, then, is in charge of getting that resource value injected into combo box items?  In other words, what is responsible for picking up a default entry such as "select a value" and adding that to the items of the NVL?  Is the object responsible since it owns the data?  Or the VM?  Or could the UI designer inject that in some way?  I haven't done anything with localization before so I'm not sure of how that fits in.  Thanks for your help.

StefanCop replied on Friday, November 04, 2011

Ok, you meant a "real" combo box, in which you also type free text; and not a drop down box to choose from a fixed list.

The view model is in charge, I suppose.

In that (first) case I think the NVL is a presentation thing and you there's nothing wrong adding an item in the view model (using the IsReadOnly = false).

 

 

TSF replied on Friday, November 04, 2011

Sorry, it would act as a drop down.  I'm using the combobox control in SL xaml.  But still, maybe what you say at the end is the right way for me to go about it.  Just set IsReadOnly = false in the object and that should the VM to add this default item to the list.  Is that what you're saying?  Thanks.

StefanCop replied on Friday, November 04, 2011

Yes.

As you might know the setter of IsReadOnly is protected. That's why you need to add the item in the BO.

Maybe you have two factory methods: standard GetList() and addition GetListWithUndefined(), something like that.

Copyright (c) Marimer LLC