Why use UseCases? Android Jetpack is not mentioning UseCases in the docs - android

Is it really necessary to use UseCases in my android Clean Architecture?
In the Android Jetpack documentation they are not mentioning it.
They are accessing the repository directly from the ViewModels.
Isn't that a better option? Isn't the UseCase code not just making it unnecessarily more difficult to adjust the code?

If you want to strictly follow Uncle Bob's clean architecture then you should use UseCases.
They are accessing the repository directly from the ViewModels. Isn't that a better option?
It depends, greatly, the clean architecture makes testing super easy, also makes you think more architecturally before you implement things and makes you not to make compromises a.k.a. adjusting code, and it follows SOLID principles which are just great.
On the other hand, it's much harder to setup project and sometimes it feels like you are overengineering it
But after setting it up you will see improvement in maintainability and also scalability.
I think that it's great to know what clean architecture is and take what suits your needs.
This is great resource if you want to learn more about clean architecture https://caster.io/courses/android-clean-architecture and how it fits android.

Well I think it's really not necessary to use UseCases, especially if you are not familiar with them. UseCases are just an architectural pattern for creating a more scalable project and to reuse code.
I am personally using UseCases where I see that it makes sense. For example in our project we have the View, ViewModel and Repository. Two common cases to use a UseCase would be if
1) Two ViewModel has a common logic of processing data from the Repository. That could go into a UseCase (But not necessarily, you can create a smaller VM for just that)
2) You want to include a plus layer between the Repository and ViewModel, because you need that layer to handle some extra logic, that is out of the purpose of the Repository and/or ViewModel. For example neither the Repository nor the ViewModel should solve scheduling problems. (A problem like, if you have cache get the data on the MainThread, if not switch to a background thread.)
So in conclusion, nothing related to architecture is necessary. You shouldn't force something on your project, architectural patterns are there only to make your application easier to modify, scale, work with.

Related

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)?
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.
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.

Should I use ViewModel with UseCase?

This is more of a question about designing applications, rather than fixing a specific issue.
So most Android tutorials I see use ViewModel as a layer between the data source and the views. Hence my first impression was that a ViewModel is supposed to handle data fetching and updating, but then I read about 'Use cases' which most Android samples don't even mention and I don't understand how all these parts fit together. What's the relationship between a ViewModel and a use case?
1) Clean Architecture this is approach how to design your application. This is not about specific realization like in case of ViewModel.
2) If you looked at the official Android documentation you will not find any mentions of Clean Architecture. Google not forcing this approach.
3) ViewModel this is part of MVVM design pattern. So if we looking info Clean Architecture MMVM can be part of Presentation layer (same as MVP commonly used in this layer). But you still need UseCase to make interactions between Data layer and Presentation layer.

Android Architecture Questions

I've been reading about architecture to android projects. And I found some stuff, but I guess I misunderstood some concepts or not even understood at all.
One of my questions is about handling api objects, if I have a local database, Should I use the same object from api to store in local database?
I'm also looking for explanation about why use MVVM or MVP, actually they looks like different stuff, I have figured out that MVP is a pattern more concerned about handling UI responsibilities, MVVM I think is oriented to handle communication between UI and database. So I misunderstood the concepts or it make sense?
The last but not least important topic is about dependency injection, I have read about the concept and this question came to my mind, why should I use any framework as dagger to handle this, if I can handle this pattern by myself, once it's not thaaat complicated?
Should I use the same object from api to store in local database?
It can really depend on how good your API object is. You should rather base your local database object on what it really mean in a logical way and if your endpoint is well done it could be the same. The important part in your architecture is to isolate your logic parts from your I/O parts (UI, database, API) so if you want to redesign your UI, change the Webservice you use it will not be too painful.
So I misunderstood the concepts or it make sense?
I am not that familiar with MVVM so I can not really answer that question. But for me the important is not to follow "by the book" one pattern or another but rather to adapt your architecture from what you like from each. I currently try do to so with the Clean Architecture. You can take a look at all the concepts Uncle Bob speak about in this article about making code cleaner and more maintainable.
why should I use any framework as dagger to handle this, if I can handle this pattern by myself, once it's not thaaat complicated?
You don't have to use dagger if you're not familiar with it. But if your project start to grow and you start to be a team of 2, 3, 5... working on it, a framework as dagger can help you keeping a common standard about how you make your dependency injection and then making the code more coherent. Dagger also provide some tools as scopes that can save you some time if you're familiar with it.

How should we deal with the growing Repository in a mvp project's model layer?

Google has launched a project on Github in order to demonstrate different architecture implementations.
todo-mvp
In its' simple MVP implementation's model layer, which applies the Repository pattern, it simply contains one POJO(Task) and we already have a bloated model layer here.
It's quite common to have dozens of POJOs in a REAL project. And it's easy to imagine how big the repository would grow.
Whenever I want to add or amend something, like adding a Owner(of a Task), I have to add so many lines of code in each one of the classes in the model layer. No need to mention that we have to write so many hard-coded SQL statements.
Is there a better way to avoid these things?
That project structure is good. If you want to save your time and avoid writing many lines of boring SQL statements, you can adopt some ORM libraries such as Realm, GreenORM etc. Same for the other stuffs.
Also checkout a new MVP framework here: http://robo-creative.github.io/mvp. With it, you can avoid writing tangled logic for view-presenter binding. The framework also supports Dependency Injection as well.

Design Pattern in Android? [duplicate]

I'm working on an Android project and I would like to know any recommendations about what's a good architecture to build an android application.
I want to use dependency injection using Roboguice and I've been reading about MVVM pattern or MVC pattern (Android MVVM Design Pattern Examples).
Also I know that roboguice have a pretty cool Context-Based Event's raising and handling feature that could be very testable as the code is decoupled.
Any recommendations on a working design pattern? a testable and scalable architecture you have worked with or developed?
The Android platform provides a common set of design patterns, and with the limited hardware resources you get compared to Web-apps it is still often best to stick with using these directly in production code. There are other frameworks that sort of "wrap" the base platform; these are worth looking into if you have a specific purpose (or perhaps for prototyping/experimenting), but for the best level of support you are generally best sticking with the standard components.
This is a great resource when working on UI solutions: http://www.androidpatterns.com/
Specifically for DI: There is a Spring framework for Android, I've had a play with it and it looks quite promising. You've already mentioned Roboguice as another alternative to this. However, to avoid performance and library overhead, I still find the easiest approach is to write a simple reflection-based class that registers and injects dependencies within my own code. Similar to this approach, except I usually move the injection code into a separate singleton and reference it from there.
In my experience most of the third-party offerings are not yet mature enough to rely on right now, and don't really give you much on top of what the base platform provides. They are constantly progressing, however, so be sure to experiment with the big names from time-to-time.

Categories

Resources