ServiceLocator
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
if true, allow registering keys multiple times. The latest registration will be used. This should generally only be true for tests.
Properties
The default LazyThreadSafetyMode used for lazy-initialized dependencies managed by the service locator.
Functions
A convenience extension to fetch a singleton instance from the ServiceLocator using its reified type T as the key.
Fetches an item for key. If no provider has been registered, this will delegate to ServiceLocator.onMiss.
A convenience extension to fetch a singleton instance from the ServiceLocator using its reified type T as the key.
Fetches the item for key. If no provider has been registered, returns null. Does not invoke ServiceLocator.onMiss.
Registers an eager singleton instance using a SingletonClassKey for the reified type T.
Registers a lazy singleton provider using a LazyClassKey for the reified type T.
Registers a lazy singleton provider for the given key.