Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About view models and interfaces

I'm developing a WPF Prism application, and everything is working fine. My view models all have interfaces, which are injected by MEF.

However, I don't really understand the benefit of interfaces for view models. After all, a view is tied to its view model, so I think there will never be other implementations.

Actually, I also have interfaces for my views. It seems that this is also overkill?

So my question is: can't I just remove all view and view model interfaces and inject the views and view models directly? Is there any reason to keep interfaces for views and view models?

Thx, L

like image 790
L-Four Avatar asked Mar 17 '11 08:03

L-Four


People also ask

What is the use of a view model?

ViewModel is one of the most critical class of the Android Jetpack Architecture Component that support data for UI components. Its purpose is to hold and manage the UI-related data. Moreover, its main function is to maintain the integrity and allows data to service during configuration changes like screen rotations.

What is the difference between View and View Model?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.

What is MVC interface?

In Spring MVC, the model works a container that contains the data of the application. Here, a data can be in any form such as objects, strings, information from the database, etc. It is required to place the Model interface in the controller part of the application.

What should view model contain?

View Model: It encapsulates the presentation logic required to support a use case or user task in the application. The view model is testable independently of the view and the model. The view model typically does not directly reference the view. It implements properties and commands to which the view can data bind.


2 Answers

It is an overkill. I understand that you may want to mock your ViewModels, but I think it's more important to be practical. Plus, why would you even need to mock your ViewModels? Any logic that needs to be mocked should be put into a service class IMHO.

like image 58
JP Richardson Avatar answered Nov 05 '22 15:11

JP Richardson


Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange your views and UI testing can be done on mocks of your ViewModel so you won't really need to interface them I think.

like image 35
Bjorn Bailleul Avatar answered Nov 05 '22 14:11

Bjorn Bailleul