Foreign key combobox in MVVM

Foreign key combobox in MVVM

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


hanspret posted on Saturday, November 13, 2010

Hi

I'm in the process of learning MVVM in Silverlight and making good process. I am however stuck on one situation. I am creating a screen where one can maintain users. When adding/updating a user record the user should link a role to a user by choosing the role from a combobox. The relationship is: One user can belong to one role and one role can belong to many users.

How would one accomplish this in the ViewModel. I have created a ViewModel for roleCollection and created an ObservableCollection in the UserViewModel that Reference the Role ViewModel but this isn't working as I hoped for. Below is the ObservableCollection in the User ViewModel:

 

 public ObservableCollection<RoleViewModel> RoleList        {            get            {                var result = new ObservableCollection<RoleViewModel>();                r = new RoleListViewModel();                //r.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(r_PropertyChanged);                                 if (r.Model != null)                        result = r.RoleList;
                return result;            }        } 

 

Can anybody please show an example how to do this? I am using bxf.

Jaans replied on Sunday, November 14, 2010

Hi Hannes

Mostly, I've done this using a Dependency Property on the ViewModel for that list of Roles and then I simply bind the ComboBox's ItemSource to that dependency property. Important thing is to ensure that the dependency property is populated before your model is populated, or the binding gets a bit unhappy.

I'm not sure about how much I'm bending the M-V-VM rules with this approach, and I wonder what others have done. I've just completed a post (before seeing your post) on the BXF discussion forums regarding this issue and questioning what the best approach is.

Please have a read and see if it relates to your situation:

http://bxf.codeplex.com/Thread/View.aspx?ThreadId=234636

Regards,
Jaans

RockfordLhotka replied on Sunday, November 14, 2010

It depends on who's definition of MVVM you listen to :)

I use two things in this scenario.

First, I use a unit of work business object to retrieve the NVL and editable business objects in the same server request. That avoids timing issues due to async in Silverlight (and I always use async in WPF too).

Second, I do what Jaans suggests, having a dependency property on my viewmodel that exposes the NVL used to populate the list or combo box. And of course my viewmodel has a Model property that exposes the editable business object.

There are variations on this theme with "parent-child" viewmodels for some more complex screens too - but this is the central theme.

It might not be "pure MVVM", but it is a simple and works well. Sometimes "purity" can make things so complex as to be useless...

Jaans replied on Sunday, November 14, 2010

Ah... OK.

That's a nice way of getting around the timing issues with multiple async retrievals - just wrap them in another BO.

Rocky, do you have an example of such a UOW amongst the samples? I guess one could simply put the "Root" objects as properties of ReadOnlyBase object?

Any pointers for organisation, naming convention, project structure regarding UOW?

PS: Just thought I'm overthinking this ... is that called mental recursion? Hmm

hanspret replied on Sunday, November 14, 2010

Thanks for the answers. 

RockfordLhotka replied on Sunday, November 14, 2010

There's some info in the FAQ (www.lhotka.net/cslanet/faq) and there's an example of UOW in the Core 3.8 video series samples.

Jaans replied on Sunday, November 14, 2010

Thanks Rocky

I found it in Core3804.wmv at position 1:39:27.

PS: Perhaps it would be nice to add timestamp to FAQ?

Jaans replied on Monday, November 15, 2010

Disclaimer: Sorry if this is included in the above references, but I could find anything about it though.

What do I use for the Model in the ViewModel?

My gut tells me the Model type of ViewModel doesn't change and that the Model and other dependency properties for the lookups / FK lists are populated with the result of the UoW factory.

My uncertainty comes in with the overloaded Refresh method on the inherted CSLA ViewModel base class. Normally you would have something like this:

BeginRefresh"Get" )

What would be appropriate considering we now have UoW to use instead?

Apologies if I'm being daft... just not seeing it at the moment.

Thank you
Jaans

 

Copyright (c) Marimer LLC