ServiceLocator

abstract class ServiceLocator @JvmOverloads constructor(allowReregister: Boolean = false)

A basic service locator for storing common dependencies. ServiceLocator acts like a heterogeneous map, holding values of different types. The ServiceKey dictates which type will be accessed.

There are several built-in key types:

  • LazyKey: holds a fixed value that is created the first time a value is requested for the key

  • SingletonKey: holds a fixed value that is created on initialization

  • ClassKey: useful for the common case where there is only one entry of a particular type. May be either Lazy or Singleton.

  • FactoryKey (experimental): used to create a new instance of the service each time a value is requested

When a key is registered in the ServiceLocator, it creates a ServiceEntry specific to that key type, which is stored for later access. When values are requested for the key, the ServiceEntry it created will be used to obtain the value. Different key types use different implementations of ServiceEntry to determine their respective behaviors.

Parameters

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(allowReregister: Boolean = false)

Types

Link copied to clipboard
class ServiceLocatorException(message: String, val key: Key<*>) : Exception

Properties

Link copied to clipboard

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

Functions

Link copied to clipboard
fun <T : Any, GetParams> get(key: ServiceKey<T, *, GetParams, *>, params: GetParams): T

Fetches an item for key. If no provider has been registered, this will delegate to onMiss.

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

A convenience extension to fetch a singleton instance from the ServiceLocator using its reified type T as the key.

fun <T : Any> ServiceLocator.get(key: ServiceKey<T, *, Unit, *>): T

Fetches an item for key. If no provider has been registered, this will delegate to ServiceLocator.onMiss.

Link copied to clipboard
fun <T : Any, GetParams> getOrNull(key: ServiceKey<T, *, GetParams, *>, params: GetParams): T?

Fetches the item for key. If no provider has been registered, returns null. Does not invoke onMiss.

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

A convenience extension to fetch a singleton instance from the ServiceLocator using its reified type T as the key.

fun <T : Any> ServiceLocator.getOrNull(key: ServiceKey<T, *, Unit, *>): T?

Fetches the item for key. If no provider has been registered, returns null. Does not invoke ServiceLocator.onMiss.

Link copied to clipboard
fun <T : Any, PutParams> put(key: ServiceKey<T, *, *, PutParams>, params: PutParams)

Registers a service in the ServiceLocator for the specified key.

Link copied to clipboard
inline fun <T : Any> ServiceLocator.put(value: T)

Registers an eager singleton instance using a SingletonClassKey for the reified type T.

inline fun <T : Any> ServiceLocator.put(threadSafetyMode: LazyThreadSafetyMode = defaultLazyKeyThreadSafetyMode, noinline provider: () -> T)

Registers a lazy singleton provider using a LazyClassKey for the reified type T.

fun <T : Any> ServiceLocator.put(key: LazyKey<T>, threadSafetyMode: LazyThreadSafetyMode = getDefaultParams().threadSafetyMode, provider: () -> T)

Registers a lazy singleton provider for the given key.