The service locator ensures the system can be built modularly by registering loosely shared dependencies and lets you control global exposure without creating multiple global objects. Instead, a single global container object holds these dependencies.
epi.dependencyimplementation is a lightweight service locator implementation to facilitate a decoupled client-side architecture.
epi/dependencyobject is a service location container responsible for managing the registration and resolution of dependencies. It is the hub class used to get references to dependent class implementations. Instead of using specific implementations in a class, use
epi.dependencyto look up the implementation. This works like a dictionary, where the key is a string and the value is an object.
The service locator lets you change the dependency at runtime or perform unit testing so you can register the dependency with the same key. You do not have to change (or even know what) objects are using that dependency.
You can register dependencies by using an object instance.
var myServiceClass = new acme.services.MyServiceClass();
You also can register a "lazy" dependency where the class implementation is created when first resolved. The registered class is required and loaded by Dojo if needed.
When resolving dependencies, you must know the key with which it is registered, then call resolve.
var service = dependency.resolve("MyServiceClass");
Updated 29 days ago