Chaining multiple use cases
Asked Answered
M

3

7

I use Clean Architecture pattern in my app together with MVVM architecture. So I have UseCases for single operations, like for example, LoginUseCase, DownloadAttachmentUseCase etc.

What I am curious about is, what if I want to chain multiple usecases in my viewmodel, do something like, login first and once it succeeds, download an attachment.

Do I need to create another separate UseCase e.g. LoginAndDownloadAttachmentUseCase ?

Mohammed answered 16/8, 2019 at 12:10 Comment(0)
E
7

Only the logic for application business rules should be in use cases; changes to the UI should not change how a use case is implemented. To me, it seems like a combined LoginAndDownloadAttachmentUseCase would only be useful in the context of a specific UI, and therefore should not exist as a use case.

Also, if you were to create combined use cases for all possible scenarios, it would lead to a combinatorial explosion of use case classes as your app becomes more complex.

It's ok to have some logic in ViewModels, especially if it's just transforming data or doing high-level operations. Each use case represents a single high-level operation. Having the ViewModel stitch together a few of these does not make testing and maintainability more difficult.

I think https://github.com/googlesamples/android-architecture/tree/usecases is a good example. The use cases are pretty slim, but in more complex apps, they could combine several datasources together.

Esch answered 16/8, 2019 at 20:50 Comment(1)
Note: Google now has recommendations on how to design your domain layer: developer.android.com/topic/architecture/domain-layerEsch
C
0

It would be good to do separate UseCase because MVVM with Clean Architecture is pretty good in such cases. It goes one step further in separating the responsibilities of your codebase. It clearly abstracts the logic of the actions that can be performed in your app.

Chiba answered 16/8, 2019 at 13:19 Comment(2)
So, what is the answer to my question ?Mohammed
Do separate UseCase, instead of combine download with loginChiba
N
-4

Try Retrofit Synchronous Requests, but Synchronous methods are executed on the main thread so they trigger app crashes on Android 4.0 or newer. You’ll run into the **NetworkOnMainThreadException** error. To handle this, execution it in a separated thread such as JobIntent Service and get the response via LocalBroadcast receiver. Upon receiving the status, you could execute your next query for downloading the attachment.

Neela answered 16/8, 2019 at 12:46 Comment(2)
This has nothing to do with my question.Mohammed
My question is about a proper way to architecture an app.Mohammed

© 2022 - 2024 — McMap. All rights reserved.