Module state to be injected in the usecases and the view models

from the CommonsWare Community archives

At April 9, 2020, 4:10am, islam.farid2100 asked:

Hi :slight_smile:
In my module I am using a singleton global state which I need to read from in the view models [ and some time I depend on some internal values in it like I check if the state.link.isEmpty()] and also I update it with some values in the UseCase like state.link = receivedLink.

1- First is it valid to inject this object and reuse it in many viewmodels in my module or not?
2- is it valid to also use it again in the usecases to edit the state values and update it.
3- Right now I need a data source to provide this state to get it from memory if existed and if not to get it from the disk cache; therefore, I will create something like StateDataSource which has two methods
getState() and updateState; however, I do not want to change the entire module because of that so I will still need to inject it directly in the viewmodels and the usecases

so is this valid to use this DataSource in the daggerModule to provide this state and whenever I want to update the state I talk to the repository to talk to the created datasource to call update method

Do you support these ideas, do you have different solutions I know there are too many questions in one question.
Please let me know if I couldn’t explain the question well.


At April 9, 2020, 11:59am, mmurphy replied:

First is it valid to inject this object and reuse it in many viewmodels in my module or not?

Singletons are valid — the repository pattern relies heavily on singletons, for example. I cannot tell you whether this specific singleton is valid. Whether injecting this specific singleton into viewmodels may depend a bit on your next question.

is it valid to also use it again in the usecases to edit the state values and update it.

If you have use cases, then I think the idea is that a viewmodel only talks to use cases. Use cases would talk to repositories or other singletons. I have not been involved in a project that made serious use of use cases, and I am not a GUI architecture expert, so my experience here is limited.

so is this valid to use this DataSource in the daggerModule to provide this state and whenever I want to update the state I talk to the repository to talk to the created datasource to call update method

To be honest, I did not understand much of this one, and I doubt that I could answer the question without a code review… and that is beyond the scope of what I offer through the Warescription.

Also, bear in mind that very little in Android GUI architecture is “set in stone”. What we have is a large set of overlapping opinions. I will tend to steer developers in the direction of what Google seems to be recommending, just because Google is in charge of Android. But that is not to say that other opinions are wrong or invalid. This is one of the reasons why I try to steer clear of architecture debates.


At April 9, 2020, 12:04pm, islam.farid2100 replied:

Thank you for your fast response :slight_smile: