https://kotlinlang.org logo
#dagger
Title
# dagger
a

André Thiele

04/04/2021, 8:39 AM
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

Colton Idle

04/04/2021, 1:19 PM
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

André Thiele

04/04/2021, 1:29 PM
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

Jason Ankers

04/04/2021, 1:48 PM
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

André Thiele

04/04/2021, 1:51 PM
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

Jason Ankers

04/04/2021, 3:24 PM
@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

André Thiele

04/04/2021, 3:25 PM
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

Jason Ankers

04/04/2021, 3:27 PM
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

André Thiele

04/04/2021, 3:29 PM
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

Jason Ankers

04/04/2021, 3:33 PM
Can you not constructor inject your auth manager into work manager?
a

André Thiele

04/04/2021, 3:34 PM
WorkManager would need to be SingletonScoped so the scopes would clash
j

Jason Ankers

04/04/2021, 3:38 PM
What scope does it have
a

André Thiele

04/04/2021, 3:39 PM
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

Carl Benson

04/07/2021, 10:21 AM
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
75 Views