A lightweight, minimal module for allowing dependency-injection in your frontend app. Written in TypeScript and recommended to use mostly in TypeScript projects that care about artifact size.
What's in it for me?
Dependency Injection (DI) is a design pattern used to implement IoC (Inversion of Control). It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.
The Dependency Injection pattern usually involve following types of classes:
Client Class: The client class (dependent class) is a class which depends on the service class.
Service Class: The service class (dependency) is a class that provides service to the client class.
Container Class: The container class is a class that associates token with the service class.
Injector Class: The injector class injects the token into the client class following by its internal logic.
Later the client will resolve the injected token from the container and receive an instance of the service or another compatible class. The following figure illustrates the relationship between these classes:
As you can see, in this approach, the DI pattern separates the responsibility of creating an object instance from the service class outside of the dependent client class, which reduces coupling between service and client classes.
micro-di is simple to use. It implements only two actions required for DI - registration and resolution of the dependencies. When used with decorators, micro-di allows building the dependency graph "automatically" inline with the class definition. Dependencies are registered in the IoC container as functions that return an object instance (ctor) and are lazily loaded (resolved only upon first access).
This library does not support cyclic dependency detection, it is up to the developer to avoid a dependency graph with cycles. Also, micro-di has only one global IoC container. Each class can have only one entry per class token. Named string tokens are unlimited but have to be unique.
To install using the npm package manager, run the following command:
$ npm install @here-mobility/micro-di --save
To install using the yarn package manager:
$ yarn add @here-mobility/micro-di
Following parameters should be enabled in your
SomeFactory as a dependency using
RegisterDependency method. This can be done anywhere in your application code:
RegisterSingleton registers a resolver function which will be resolved to the constructed instance the first time
SomeClass dependency is accessed. After the first time, the resolved instance will be stored in the container and subsequent access to
SomeClass will always return the same instance as a Singleton.
It is possible to register dependencies by using Experimental-Decorators. The same two options are available with decorators.
Resolvable decorator registers resolver function as is:
;;// Register multiple distinct dependencies
Singleton decorator will register a single instance of this object:
Dependency can be resolved by using
Resolve method. This can be done anywhere in your application code:
;;;; // String token can be resolved to any type
Dependency can be injected to constructor arguments automatically:
Dependency can also be injected by using experimental-decorator
Overriding Dependencies in tests
Dependencies in unit tests can be simply overridden with relevant mocks without touching the properties of the actual object under test. Use
OverrideSingleton methods where applicable accordingly:
;;OverrideSingletonLocator,new MockLocator;OverrideResolverNameFactory,new MockFactory;
Using in the test case: