Locating ViewModel and UI Projects

I am writing a wpf project and using the MVVM paradigm I was wondering if I should write view model classes in my own project.

The benefits I see is that your ui project should never be aware of your business logic. (don't link to it)

however, if I want to use the ICommand interface in my view model, I still need a reference to the PresentationCore, which can indicate that I should be in my ui project.

comments are most appreciated.

+2


source to share


2 answers


I don't think this is an irresistibly compelling way to do it anyway. I try to keep VMs and their views in one assembly, but in a different folder structure. For example, I might have ViewModels / Foo / Bar / CustomerViewModel and Views / Foo / Bar / CustomerView.xaml.



I don't think there is an issue separating views and view models. It is natural to have sub-assemblies associated with a VM build such as PresentationCore. After all, your view models are part of your presentation tier.

+1


source


Once you reference the PresentationCore in your ViewModel (which is currently unavoidable if you want to use ICommand), you are missing out on all sorts of unwanted View related functionality in your ViewModel. For example MessageBox.Show, when you see this is called in a unit test, it leads home why it's bad.

To answer the question, keep the View and ViewModel in separate projects. I thought about this myself, but after going down a separate project it took me a while to evaluate, but it was very helpful to get me to stick with a pure MVVM solution and the lessons from this greatly improved my overall solution architecture. It's all about reducing the dependencies on unnecessary assemblies by using interfaces and optionally an adapter template. One example is that my entry-point View project has a reference to Ninject, but I don't want my ViewModel to have that reference. Otherwise, someone might come along and use Ninject's static core directly.



As far as ICommand is concerned, I don't know that history has only used WPF since 4, but it looks like MVVM was overdue. I'm not sure why else Microsoft would put this interface in PresentationCore. I hope this will be covered in the next release with a separate assembly for the ViewModel layer.

+2


source







All Articles