Skip to content

Inject a service into a controller ​

Suppose two endpoints need the same catalog. Declare one provider and ask CHO to pass it to each controller. This makes the lifetime explicit and gives a test a place to substitute a different catalog.

ts
import { Args, Controller, Get, Injectable, Module, Param } from "@chojs/web";

@Injectable()
class Catalog {
  readonly titles = new Map([["42", "The Odyssey"]]);
}

@Controller({ route: "books", dependencies: [Catalog] })
class Books {
  constructor(readonly catalog: Catalog) {}

  @Get(":id")
  @Args(Param("id"))
  find(id: string) {
    const title = this.catalog.titles.get(id);
    return title === undefined
      ? new Response("Unknown book", { status: 404 })
      : { id, title };
  }
}

@Module({ providers: [Catalog], controllers: [Books] })
export class Library {}

Trace the dependency ​

  • providers: [Catalog] gives the module a declaration it can instantiate. @Injectable() describes how to build the class.
  • dependencies: [Catalog] tells CHO which token to resolve for the first constructor argument. The compiler checks the constructor against that list.
  • controllers: [Books] registers the HTTP entry point. When creating Books, CHO looks up Catalog in the module that declares Books.

Within an application, callers resolving the same declaration receive the same instance. A controller can therefore keep a reference to the catalog. Keep request-specific data on the request context.

Choose the next step ​

RequirementMechanism
Inject a string, interface or asynchronously constructed clientTyped tokens and factory providers
Use one catalog across featuresExport it from a module
Release a client or replace it during a testProvider lifecycle and overrides
Share instances between HTTP and message handlersUse one injector

Released under the MIT License.