Skip to content

Components

Components are the architectural glue of DolphJS. They define how Controllers and Services are bound together and instantiate the Dependency Injection (DI) system.

A Component is simply a class decorated with @Component(). The decorator accepts a configuration object defining which controllers and services belong to this logical module.

src/order/order.component.ts
import { Component } from '@dolphjs/dolph/decorators';
import { OrderController } from './order.controller';
import { OrderService } from './order.service';
import { TransactionService } from './transaction.service';
@Component({
controllers: [OrderController],
services: [OrderService, TransactionService],
})
export class OrderComponent {}

When the DolphFactory boots up and receives an array of Components:

  1. It reads the metadata attached to OrderComponent.
  2. It instantiates the OrderService as a singleton instance.
  3. It instantiates the OrderController.
  4. It reflects upon the properties of OrderController and maps the OrderService singleton directly into the controller’s property.
  5. It resolves the @Route definitions inside OrderController and mounts them to the underlying Express instance.

Once you have defined your components, you must register them in the central entry point of your application using DolphFactory.

src/server.ts
import { DolphFactory } from '@dolphjs/dolph';
import { OrderComponent } from './order/order.component';
import { UserComponent } from './user/user.component';
// Pass all root components into the factory
const dolph = new DolphFactory([OrderComponent, UserComponent]);
dolph.start();