Should the view model contain logic?

In a wpf application, What is the responsibility of the viewmodel? can he manage everything or represent only the presentation and send messages / events to the business layer and receive information from him?

+6
wpf mvvm
source share
1 answer

The short answer is yes.

Longer answer ...

The main goals of the Model-View-ViewModel (MVVM) template:

  • Allow unit testing of your view logic. These are unit tests applied to a ViewModel that runs without a view associated with it.
  • Lighten the workflow of the development developer by minimizing the amount of code associated with your XAML files.

The MVVM pattern also provides a separation of concerns between presentation logic and business logic, just like MVC and their user interface pattern. However, 2 points above is what the MVVM pattern really defines.

Now think about where you will find your business logic. If you put it in your ViewModel, are # 1 and # 2 still true? Yes. If you place it on a separate layer, are there still # 1 and # 2? Yes.

Therefore, in both cases, you still achieve the two main goals of MVVM. Which route you really use depends on the complexity of your application and the number of developers working on it. As both of these factors increase, it will be useful for you to have 3 layers ... or more!

+8
source share

All Articles