views:

32

answers:

1

Hi

I have a DomainView that allows you to select an Entity in my domain. The Entity is displayed in an EntityView within the DomainView.

My question is what should the 'DomainViewModel' property be that the EntityView binds to?

  • The Entity, with the View itself wrapping it up in a EntityViewModel and it binds to that?
  • The Entity, using a Converter on the binding to convert between the Entity and EntityViewModel?
  • An EntityViewModel, created by the DomainViewModel?

All would work, I just wondered what would be the 'MVVM-way'? My preference would be for one of the last two.

Lee

A: 

With the typical 'MVVM-way', ViewModels shouldn't be aware of other ViewModels and the relationship between View and ViewModel is 1-1.

Sounds like your real question is "How do I communicate data between ViewModels"? A common Master/Details interaction.

Are you using any frameworks? I am personally more familiar with PRISM, but the concepts are similar in MVVM Light and others. In PRISM, a good solution is the EventAggregator. The DomainViewModel publishes an "EntitySelected" aggregate event that is subscribed to by the EntityViewModel.

Another option is to inject a common service (or model, depending on your style) into both ViewModels. That service would provide a public property like CurrentEntity that is set by the DomainViewModel as needed.

Either would provide a mechanism of communicating data between the ViewModels without those ViewModels having any dependency on each other.

Brandon Copeland
Thanks - at the moment, I'm not using a framework, just Silverlight 4.0. MVVM as a practise is still fairly new to me - even though I have been working that way for a while - I hadn't realised it was MVVM! I'll investigate your ideas, but if anyone as any other suggestions, please let me know.
Lee Atkinson