I am working on a new app and I am planning to use...
# dagger
s
I am working on a new app and I am planning to use dagger. I am specifically interested to know your thoughts on dagger components. 1. Should I create components based on the lifecycle of their use case. For example, a component for all objects when the user is logged in? I am confused about how to structure my components. 2. Where should components be stored? For example, for the user logged in component, where do I store the component? 3. How do I dispose of components? Is setting it to null enough? Online posts do not go into the details of how to structure components for large projects. So I am having a hard time, how many components to create and for what use cases. Please assist. If there is a better channel for this question let me know.
s
For 1 and 2, there is an much larger discussion, related to scoping Dagger components. For 3, if your components have a lifecycle that uses CoroutineScopes (and maybe other types of lifecycle owners), you'd need to explicitly cancel (dispose of) them. The JVM garbage collector will not magically cancel a CoroutineScope, for example.
a
re 1. I have been using that approach successfully for years. 2. In store mine in a @ Singleton holder since we only support one session. 3. Yes, except if you created stuff like Anton pointed out.
s
@streetsofboston @ade Where do you store your custom scoped components?
a
In a class scoped as singleton, on the global component. It's responsible for creating, persisting, recreating and destroying the session when logging in/out or restarting app.
s
So all components regardless of scope are stored in this class?
a
No, i was talking specifically about the "logged in scope" component.
s
Does it make sense to think of components as bags of items? While modules are shops? Bags may have a lifecycle while shops are static?
a
Oof, not sure what to say. I'm sure the dagger docs can describe it better than me. But yes in a sense components are containers of stuff. They are stateful. I wouldn't say modules are static because they can be created and contain state too.
I can recommend this playlist to get an initial understanding of stuff in dagger2, https://www.youtube.com/playlist?list=PLrnPJCHvNZuA2ioi4soDZKz8euUQnJW65