Components
Components are the architectural glue of DolphJS. They define how Controllers and Services are bound together and instantiate the Dependency Injection (DI) system.
The @Component Decorator
Section titled “The @Component Decorator”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.
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 {}How Components Work Internally
Section titled “How Components Work Internally”When the DolphFactory boots up and receives an array of Components:
- It reads the metadata attached to
OrderComponent. - It instantiates the
OrderServiceas a singleton instance. - It instantiates the
OrderController. - It reflects upon the properties of
OrderControllerand maps theOrderServicesingleton directly into the controller’s property. - It resolves the
@Routedefinitions insideOrderControllerand mounts them to the underlying Express instance.
Bootstrapping Components
Section titled “Bootstrapping Components”Once you have defined your components, you must register them in the central entry point of your application using DolphFactory.
import { DolphFactory } from '@dolphjs/dolph';import { OrderComponent } from './order/order.component';import { UserComponent } from './user/user.component';
// Pass all root components into the factoryconst dolph = new DolphFactory([OrderComponent, UserComponent]);
dolph.start();