WPF/MVVM - keep ViewModels in application component or separate?

a 夏天 提交于 2019-12-24 05:14:05

问题


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


回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!