How scope a database to a logged in user using Hil...
# dagger
a
How scope a database to a logged in user using Hilt? The problem is that a database should also be kind of singleton scoped but also scoped to a new user when logging out and in.
1
c
Scopes used to confuse me a lot and I think Jake explained it well once, but basically... Only you know the lifetime of a scope such as "UserLoggedIn" and so if you just create a new scope, then instantiate it when the user is logged, then get rid of it on log out, and that should be what you need. Just a heads up that I've never created a custom scope actually, but I read into it a lot when I tried doing the same thing that you're doing. I wanted a UserLoggedIn scope and UserLoggedOut scope but a few different questions online ended up talking me out if it. I think Uber had done this in their RIBS architecture.
a
Yeah I'm experiencing the same. I can't even find proper samples and this is a common problem in a real world application
1
Currently building a custom component
j
Hey I recently implemented an AuthenticatedScope with dagger. If you’re using Hilt a great starting point is https://medium.com/androiddevelopers/hilt-adding-components-to-the-hierarchy-96f207d6d92d
a
Yes I am referring to this right now, but i just learned that WorkManager cant access dependencies of custom components
sooo i dont really know what I should do now
@Jason Ankers would you mind sending me a gist of it?
j
@exfalso What is it in particular you aren’t sure about? It’s basically the same as the article but I added some extension functions to make injecting less verbose
Dont use WorkManager so unfortunately cant offer much help there
a
yea i was just wondering because if i build a AuthComponent with my DB, then every other dependency would have to get manually injected using EntryPoints and this would be super messy
because all repositories access the db and would have to be scoped to the same component
j
Yeah most of our repositories are auth scoped as well. I made them extension properties on the component, so injecting looks like:
Copy code
private val userRepository = authManager.component.userRepository
No idea if this is the right thing to do or not, I asked about it here https://kotlinlang.slack.com/archives/C5FT9Q36G/p1615776557029500
a
mhmm
i would need to replace constructor injection everywhere
just for one db that should be scoped
seems weird to me
also, work manager needs to access the dependencies of the custom components which is not possible as far as i know
j
Can you not constructor inject your auth manager into work manager?
a
WorkManager would need to be SingletonScoped so the scopes would clash
j
What scope does it have
a
I think WorkManager has to be Singleton, and my repositories would be scoped to AuthComponent which is technically one layer below Singleton so the dependency is not available to the WorkManager
"You can use only 
@Singleton
 or unscoped bindings in 
Worker
 objects. You must also annotate the 
Context
 and 
WorkerParameters
 dependencies with `@Assisted`:"
@Jason Ankers do you know if its possible to remove it from the constructor injection and inject it using your extension?
c
amazing, I had exact this problem last week but decided to implement explicit database closing when logging out. This is much nicer, will adopt 🙏
👍 1
156 Views