Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton and LazySingleton support #187

Closed
NicolaVerbeeck opened this issue Feb 4, 2022 · 5 comments
Closed

Singleton and LazySingleton support #187

NicolaVerbeeck opened this issue Feb 4, 2022 · 5 comments

Comments

@NicolaVerbeeck
Copy link

It would be great if we could have singletons and lazy singleton support

@evant
Copy link
Owner

evant commented Feb 4, 2022

Can you be more specific on what you are looking for? You can already inject object and use scopes to retain instances.

@NicolaVerbeeck
Copy link
Author

I must be missing the point of the scopes in that case. I was looking for something like:

@LazySingleton
@Inject
class UserRepository internal constructor(
    private val userApi: UserApi,
)```

@evant
Copy link
Owner

evant commented Feb 4, 2022

Scopes live as long as their component, so you can do this by having a single top-level component.

@Scope
annotation class Singleton

@Singleton
@Inject
class UserRepository

@Singleton
@Component
abstract class SingletonComponent {
  abstract val userRepo: UserRepository

  companion object {
    val Instance = SingletonComponent::class.create()
  }
}

@evant
Copy link
Owner

evant commented Feb 4, 2022

And scoped dependencies are instantiated when they are first accessed so I guess they are all lazy?

@NicolaVerbeeck
Copy link
Author

I'll take a look, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants