I am following MVVM pattern and have my Views in Solution.UserInterface assembly and ViewModels in Solution.BusinessLogic assembly. As far as I know, ViewModels should know nothing about Views and Views should hold reference to ViewModels. However, what if I would like to open my custom View from ViewModel from Solution.BusinessLogic? (for example, Insert new item window). I can't because that would mean to add a reference to Views and therefore cause a circular dependency. How to solve this issue?
-
Possible duplicate of [Opening new window in MVVM WPF](http://stackoverflow.com/questions/25845689/opening-new-window-in-mvvm-wpf) – ASh May 13 '17 at 13:18
1 Answers
I think you are mixing up a few things here. MVVM has three parts: The Model, the View and the ViewModel. The View represents your data and is bound to your ViewModel. Both of these usually exist in the same User Interface assembly like Solution.UserInterface. In a View-first approach the ViewModel knows nothing about its associated View(s) - this is correct. But it can easily work with other Views like showing a new window that is bound to another ViewModel.
The Model is the place where your business logic should reside. This could very well be in another assembly. The ViewModel holds a reference to the business object and possibly converts it to another format to let the associated View(s) show it in a readable format.
Please also refer to this question for clarification: Should I implement business logic on a Model or a ViewModel
-
Isn't a View User Interface and ViewModel/Model a Business Logic? Why should businness logic reside in User Interface layer? – OpenMinded May 13 '17 at 17:36
-
1No, in MVVM the ViewModel is used as the DataContext for the View, to let you bind your controls to properties. This is done in the UI layer. The ViewModel gets its data from the Model which is in your business logic layer. Have a look at https://msdn.microsoft.com/en-us/library/hh848246.aspx to get further info about MVVM. – Raziel May 13 '17 at 17:51
-
1@OpenMinded If this was helpful to you I would be glad if you marked it as the accepted answer. – Raziel Jun 14 '17 at 13:55