2

I have a WPF application where I'm using the MVVM pattern.

I get the VM activated for actions that require user input and thus need to activate views from the VM.

I've started out separating the VMs into a separate component/assembly, partly because I see them as the unit testable part, partly because views should rely on VM, not the other way round. But when I then need to bring up a window, the window is not known by the VM.

All introductions I find place the VM in the WPF/App component, thus eliminating the problem.

This article recommends keeping them in separate layers : http://waf.codeplex.com/wikipage?title=Architecture%20-%20Get%20The%20Big%20Picture&referringTitle=Home

As I see it, I have the following choices

  1. Move VMs to the WPF/App assembly to allow VMs to access the windows directly.

  2. Place interfaces of views in VM-assembly, implement views in WPF/App assembly and register the connection through IOC or alternative ways.

  3. File a 'request' from the VM into some mechanism/bus that routes the request (but which mechanism!? E.g something in Prism?!)

What's the recommendation?

Thanks for any comments,

Anders, Denmark

Anders Juul
  • 2,407
  • 3
  • 34
  • 56

2 Answers2

2

Don't pick option 1. You'll be adding an unwanted dependency from VM to V.

Options 2 and 3 are both valid and being used. Picking between these is sometimes a matter of taste. I think that IOC enables/allows mocking better whereas a messagebus works fine for small applications.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Interesting - I never have used a message bus for something like this but would have thought it was for larger systems? As for the rest, points taken, and thanks! – Anders Juul Jan 15 '11 at 17:18
  • It can be a simple class that listens for messages and knows where to send them (publisher/subscriber pattern) You have to be careful in using them as they might keep Views from being collected when you store references to them in the bus. Some suggest using weakreferences to solve that. – Emond Jan 15 '11 at 17:57
  • Hi, It's pretty much a tie as to which answer to choose. I ended up using Prism.EventAggregator to tie things together. Thanks Erno – Anders Juul Jan 19 '11 at 12:39
0

Keep your ViewModels in a separate assembly from the Views.

If you look into Cinch and MEFedMVVM you'll see a very powerful mechanisms for connecting views and viewmodels using MEF. Keeping them separate facilitates running your application headless (no UI), which is great for testing and exposing an API.

Zamboni
  • 7,897
  • 5
  • 43
  • 52