A modular application framework with dependency injection. Based on @angular application structure.
npm i @uon/core
Application modules are defined with the @Module decorator.
import { Module } from '@uon/core';
@Module({
imports: [],
providers: []
})
export class MyModule {}
Declaring a list of providers make them available to the dependency injector.
Providers are referenced my their token. A token can be any comparable value; however, for code minification purposes, we recommend using either an InjectionToken or a type (class).
There are multiple ways of declaring a provider :
import { Module, InjectionToken } from '@uon/core';
export const MY_VALUE_PROVIDER =
new InjectionToken<number>("MY_VALUE_PROVIDER");
@Module({
imports: [],
providers: [
// TypeProvider
MyService,
// ValueProvider
{
token: MY_VALUE_PROVIDER,
value: 1234
},
// FactoryProvider
{
token: 'MY_FACTORY_PROVIDER',
factory: (service: MyService) => {
return service.getStuff();
},
deps: [MyService]
},
// ClassProvider
{
token: MyClass,
type: MyOverrideClass
}
]
})
export class MyModule {}
You can provide a type as a provider. The injector will instanciate the class with DI the first time it is requested.
// MyService.ts
import { Injectable } from '@uon/core';
@Injectable()
export class MyService {
constructor(private myOtherService: OtherService) {}
}
stub
stub
If you wish to execute tasks before the application modules are instanciated. You can add a factory provider with the APP_INITIALIZER injection token.
import { Module, APP_INITIALIZER } from '@uon/core';
@Module({
providers: [
{
token: APP_INITIALIZER,
factory: () => {
console.log('do stuff');
},
multi: true
}
]
})
export class MyModule {}
More coming soon.