Skip to content

Dependency injection

If you want to use Dependency injection for your services, you can by defining your services in src/app/services:

import { Injectable } from "@fehujs/ioc"

@Injectable()
export class MyService extends BaseService {
    getData() {
        return { data: "my data" }
    }
}

The Injectable decorator permits to define a class that could be injected.

Then in your controller:

import { Inject } from "@fehujs/ioc"

export class TestDIController extends BaseController {
    constructor(@Inject(MyService) protected myService: MyService) {
        super()
    }

    myView({ response }: HttpContext) {
        return response.setResponse({
            body: JSON.stringify(this.myService.getData()),
            contentType: "application/json"
        })
    }
}

The Inject decorator defines a dependency of the class.

You can register the views like all controllers.

Tip

You can inject services into services:

@Injectable()
export class SecondService extends BaseService {
    getData() {
        return "hello, world"
    }
}

@Injectable()
export class FirstService extends BaseService {
    constructor(@Inject(SecondService) protected secondService: SecondService) {
        super()
    }

    getData() {
        return { data: this.secondService.getData() }
    }
}

And then in the controller:

export class TestDIController extends BaseController {
    constructor(@Inject(FirstService) protected firstService: FirstService) {
        super()
    }

    myView({ response }: HttpContext) {
        return response.setResponse({
            body: JSON.stringify(this.firstService.getData()),
            contentType: "application/json"
        })
    }
}

Tip

You can inject as many services as you want.

export class TestDIController extends BaseController {
    constructor(
        @Inject(FirstService) protected firstService: FirstService,
        @Inject(SecondService) protected secondService: SecondService,
    ) {
        super()
    }

    myView({ response }: HttpContext) {
        console.log(this.secondService.getData())

        return response.setResponse({
            body: JSON.stringify(this.firstService.getData()),
            contentType: "application/json"
        })
    }
}