问题
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
Move VMs to the WPF/App assembly to allow VMs to access the windows directly.
Place interfaces of views in VM-assembly, implement views in WPF/App assembly and register the connection through IOC or alternative ways.
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/4698702/wpf-mvvm-keep-viewmodels-in-application-component-or-separate