Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using interfaces for ViewModels?

I am currently integrating architecture components into my app according to the official documentation and the sample apps provided by google (sunflower and todo-app). I realized that none of these use interfaces for ViewModels (the sunflower app does not even use interfaces for repositories).

My question is: is it reasonable to just omit the interfaces for ViewModels (including advantages and disadvantages)?

like image 544
Doflaminhgo Avatar asked Sep 26 '18 14:09

Doflaminhgo


People also ask

Should I create an interface for my view model?

If you are never going to create more than one implementation of an interface (including mocking), then there is no need to create the interface, so you can reduce the code and the overall maintenance burden. Can you unit test your view model, even without the interface (answer should be yes, otherwise you have some other problems IMO)

What is the use of ViewModel in Android Studio?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. Note: To import ViewModel into your Android project, see the instructions for declaring dependencies in the Lifecycle release notes.

How do I create an interface for a customereditviewmodel?

In the ViewModels directory, open the CustomerEditViewModel.cs file. Create an interface for CustomerEditViewModel by clicking the class name, right-clicking, selecting Quick Actions and Refactorings, then Extract interface. When the Extract Interface box appears, change the selection for Select destination to Add to current file and click OK.

What is the purpose of the ViewModel class?

The ViewModel class allows data to survive configuration changes such as screen rotations. Note: To import ViewModel into your Android project, see the instructions for declaring dependencies in the Lifecycle release notes.


2 Answers

Is it reasonable to just omit the interfaces for ViewModels?

The below is quite general and applicable not just for ViewModels.

Advantages:

  • less code

Disadvantages:

  • won't be able to use most of the well-known design patterns;
  • won't be able to properly unit test classes (no mocking);
  • won't be able to properly use dependency injection frameworks;
  • code refactoring when using another concrete implementation.
like image 148
Onik Avatar answered Sep 27 '22 20:09

Onik


The answer depends on the complexity of your ViewModel. If you are never going to create more than one implementation of an interface (including mocking), then there is no need to create the interface, so you can reduce the code and the overall maintenance burden.

That said the important things to consider are:

  • Can you unit test your view model, even without the interface (answer should be yes, otherwise you have some other problems IMO)
  • Can you still use a dependency injection framework (the answer is yes at least for some DI frameworks like Prism)
  • Are you only ever going to create one implementation of your ViewModel?

I believe that the mark of a well-designed ViewModel, should have a relatively simple implementation, and be easy to unit-test without having to resort to mocking.

like image 37
cdiggins Avatar answered Sep 27 '22 21:09

cdiggins