The service locator ensures that the system can be built in a modular manner by registering dependencies shared in a loosely coupled fashion, and lets you control exposure globally without creating multiple global objects. Instead, a single global container object holds these dependencies.
- The epi.dependency implementation is a lightweight service locator implementation to facilitate a decoupled client-side architecture.
- The epi/dependency object is a service location container that is responsible for managing the registering and resolving of dependencies. It is the hub all classes use to get references to dependent class implementations. Instead of using specific implementations in a class, use epi.dependency to look up the implementation. This works just like a dictionary, where the key is a string and the value is an object.
Server locator lets you change the dependency at runtime or to perform unit testing, so you can register the new dependency with the same key. You do not need 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(); dependency.register("MyServiceClass", 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 it comes to resolving dependencies, you need to know the key with which it is registered, then call resolve.
var service = dependency.resolve("MyServiceClass");
Updated 7 months ago