Usage Guide
IMPORTANT NOTE: Don't forget to annotate your classes with the
@Service
decorator! Both the ones being injected and those which requests the dependencies should be annotated.
Registering dependencies
There are three ways to register your dependencies:
annotating a class with the
@Service()
decorator (documentation)registering a value with a
Token
registering a value with a string identifier
The Token
and string identifier can be used to register other values than classes. Both tokens and string identifiers can register any type of value including primitive values except undefined
. They must be set on the container with the Container.set()
function before they can be requested via Container.get()
.
For detailed documentation about @Service
decorator please read the @Service decorator page.
Injecting dependencies
There are three ways to inject your dependencies:
automatic class constructor parameter injection
annotating class properties with the
@Inject()
decoratordirectly using
Container.get()
to request an instance of a class,Token
or string identifier
Constructor argument injection
Any class which has been marked with the @Service()
decorator will have it's constructor properties automatically injected with the correct dependency.
TypeDI inserts the container instance which was used to resolve the dependencies as the last parameter in the constructor.
Property injection
Any property which has been marked with the @Inject
decorator will be automatically assigned the instance of the class when the parent class is initialized by TypeDI.
For detailed documentation about @Inject
decorator please read the @Inject decorator page.
Using Container.get()
Container.get()
The Container.get()
function can be used directly to request an instance of the target type. TypeDI will resolve and initialize all dependency on the target class. Container.get()
can be used to request:
a constructable value (class definition) which will return the class instance
a
Token
which will return the value registered for thatToken
a string which will return the value registered with that name
For detailed documentation about Token
class please read the Service Tokens page.
Singleton vs transient classes
Every registered service by default is a singleton. Meaning repeated calls to Container.get(MyClass)
will return the same instance. If this is not the desired behavior a class can be marked as transient
via the @Service()
decorator.
Last updated