Description Inversion of Control.
Inversion of control is a very versatile tool that helps you manage
dependency injection.
One of the great advantages of @dreamkit/ioc is that it does not use
decorators, is fully typed, and allows you to manually inject dependencies
for testing purposes.
DreamKit uses it extensively and hands over part of its management to the
user, allowing the user to consume only the dependencies they need and/or
create their own dependencies.
Import Definition useFactory ?: ( $context : typeof context , key : any ) => any ;
unregister ( key : any ) : typeof context ;
resolve ( key : any ) : unknown ;
Examples Basic usage import { $route, context, IocClass } from " dreamkit " ;
abstract class ThirdModel {
abstract fetch () : string ;
class MyModel extends IocClass ({ ThirdModel }) {
return this . thirdModel . fetch ();
export default $route . path ( " / " ) . create ( () => {
class ThirdModelImpl extends ThirdModel {
const manualModel = new MyModel ( { thirdModel: new ThirdModelImpl () } );
const autoModel = context
. register (ThirdModel, { value: new ThirdModelImpl () })
< p >{manualModel.fetch()} </ p >
< p >{autoModel.fetch()} </ p >
Optional parameter import { context, IocClass, iocParam, ServiceClass } from " dreamkit " ;
toUpperCase ( value : string ) {
return value . toUpperCase ();
class MyModel extends IocClass ({
string: iocParam (StringModel) . optional (),
toUpperCase ( value : string ) {
return this . string ?. toUpperCase (value);
export class MyService extends ServiceClass ({}) {
const model1 = new MyModel ( {} );
const model2 = new MyModel ( {
return value . toUpperCase () ;
. register (StringModel, { useFactory : () => new StringModel () })
model1: model1 . toUpperCase ( " text " ), // undefined
model2: model2 . toUpperCase ( " text " ), // "TEXT"
model3: model3 . toUpperCase ( " text " ), // "TEXT"