Needle
A standalone annotation based dependency injection framework
Declaring Class Dependencies
Tag properties for injection with @inject(providerName : string)
Registering Providers
An injection is only resolved when something can provide a value for it. A provider's job is to 'provide' a value for an injection. This can happen synchronously or asynchronously.
Synchronous Providers
A provider can be registered synchronously via Injector.provider(providerName : string, implementation : any);
.
for example, to synchronously provide engines
in the above example we could write:
Injector.provide"engines", ;
However, the order that the injection and the provider it is looking for are declared in does not matter, the injection wont resolve until the provider has resolved.
Asynchronous Providers
A provider can also be registered asynchronously, for example you may have a provider that needs to make an http call before it can provide a value to be injected. Heres how that works:
Injector.provideAsync'warheads', ;
Providers with Dependencies
Often you will want a provider to depend on other providers. This works in much the same way as the @inject
annotation in that the implementation funciton won't run until all dependencies for your provider are resolved. (Note that if you have any dependency cycles you will get an exception)
//provide a blue laser colorInjector.provide'laserColors.blue', new Color0, 0, 255; //provide a set of lasers using the blue color, this provider wont run its function until `laserColors.blue` has resolved.//note that the function arguments map ordinally to the array of provider namesInjector.provideAsync'lasers', , ;
Creating Instances
To create an instance of class that you have @inject
annotations on, use Injector.create(type : typeof Type) : Promise<Type>
//using XWing class from aboveInjector.createXWing.then
Injecting Dependencies
If you already have an instance of something you can re-inject dependencies with
Injector.injectDependencies<T>(instance : T) : Promise<T>
Mocks
Mocks provide a way to swap out dependencies for testing or development. Before we defined a warheads
provider that makes an http call to get a list of warheads to add to our XWing. When testing we may want to mock that call out. We can do so by using Injector.mock(providerName : string, mockName : string, implementation : any)
Injector.mock'warheads', 'mockedWarheads',
we can then apply this mock with Injector.useMock('warheads', 'mockedWarheads')
. Now anytime we inject warheads
we get our mocked array back, not the server response.
We can revert to the actual implementation with Injector.useActual(providerName : string)
Overrides
Both create
and injectDependencies
can take an optional override object.
Injector.provide'warheads', ; //override warheads provider to return concussion missiles instead Injector.createXWing, .then; Injector.createXWing.then;