Dependency Injection

KambojaJS allows you to use any dependency injection framework. You are free to choose any server side javascript dependency injection you like.

Note custom dependency injection is optional in KambojaJS if you not comfortable with it, you are free to not to use it.

Example Code You can check dependency injection sample using KambojaJS and InversifyJS here

KambojaJS provide a Dependency Resolver, its an extension point for you to override KambojaJS behavior on resolve and create object dependency.


To create a dependency resolver, you create a class implement DependencyResolver interface below:

export interface DependencyResolver {
    resolve<T>(qualifiedClassName: string);

The implementation then will be like below:

import { Core } from "kamboja"

export class MyCustomDependencyResolver implements Core.DependencyResolver{
    constructor(private myDiContainer)

    resolve<T>(qualifiedClassName: string){
        return this.myDiContainer.resolve<T>(qualifiedClassName)

Above code showing example of custom dependency resolver by passing the DI container in the custom dependency resolver.


Registering dependency resolver is easy, you do this in the KambojaJS application.

import { KambojaApplication } from "kamboja"
import { MyCustomDependencyResolver } from "./my-custom-dependency-resolver"

//setup your DI Container, or do it in separate module
let myDiContainer = new YourDiContainer();

let kamboja = new KambojaApplication(__dirname)
    .set("dependencyResolver", new MyCustomDependencyResolver(myDiContainer));
let app = kamboja.init()

Identifier Resolver

By default it is required to use qualified class name as an ID of your class, because the request handler need to know the id/name of appropriate controller classes in real time. By default it have no idea what is the id or name of the controller you specified on your dependency injection.

Note if you are OK using qualified class name as the ID of classes in your dependency injection setup, you don’t need to define an identifier resolver.

Technically identifier resolver is a converter from qualified class name to id you choose vice versa. Below is the interface of identifier resolver.

export interface IdentifierResolver {
    getClassId(qualifiedClassName: string)
    getClassName(classId: string)

getClassId should provide the id based on qualified class name provided, and getClassName should provide the qualified class name base on id provided.

To register your identifier resolver is the same as registering dependency resolver, by provide your identifier resolver in KambojaJs application

let kamboja = new KambojaApplication(__dirname)
    .set("identifierResolver", <your custom identifier resolver>);
let app = kamboja.init()