SimpleServiceLocator

open class SimpleServiceLocator<out S> @JvmOverloads constructor(scope: S, allowReregister: Boolean = false)

A service locator designed for the common use case of managing lazy, class-keyed singletons.

This class allows services to be registered and retrieved using their class type as an identifier, providing a straightforward way to manage application-wide dependencies.

Parameters

S

The type of the scope identifier.

scope

The specific scope instance for this locator.

allowReregister

if true, allow registering keys multiple times. The latest registration will be used. This should generally only be true for tests.

Constructors

Link copied to clipboard
constructor(scope: S, allowReregister: Boolean = false)

Properties

Link copied to clipboard

The default LazyThreadSafetyMode used for lazy-initialized dependencies managed by this service locator.

Link copied to clipboard
val scope: S

Functions

Link copied to clipboard
inline fun <T : Any> get(): T

Fetches the singleton instance for the reified type T. If no provider has been registered, this will delegate to onMiss.

fun <T : Any> get(key: KClass<T>): T

Fetches the singleton instance for the given class key. If no provider has been registered, this will delegate to onMiss.

Link copied to clipboard
inline fun <T : Any> getOrNull(): T?

Fetches the singleton instance for the reified type T. If no provider has been registered, returns null. Does not invoke onMiss.

fun <T : Any> getOrNull(key: KClass<T>): T?

Fetches the singleton instance for the given class key. If no provider has been registered, returns null. Does not invoke onMiss.

Link copied to clipboard
inline fun <T : Any> getOrProvide(noinline allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, noinline provider: (S) -> T): T

Fetches the singleton instance for the reified type T.

fun <T : Any> getOrProvide(key: KClass<T>, allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, provider: (S) -> T): T

Fetches the singleton instance for the given class key.

Link copied to clipboard
inline fun <T : Any> put(threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, noinline provider: () -> T)

Registers a lazy singleton provider for the reified type T.

fun <T : Any> put(key: KClass<T>, threadSafetyMode: LazyThreadSafetyMode = defaultThreadSafetyMode, provider: () -> T)

Registers a lazy singleton provider for the given class key.