Dependency Injection
Backend applications often need a solid architecture patterns to manage class dependencies. Entropy provides a common dependency injection (DI) concept implementation.
Basic Usage
In order to use dependency injection, use the inject
function. You may use it to create singleton instances of services you create.
src/root.controller.ts
import { Controller } from '@entropy/router'; import { UserService } from './users/user.service.ts'; export class RootController extends Controller { private readonly userService = inject(UserService); // ... }
Services
We strongly encourage developers to maintain clean code structure divided into small parts (modules, controllers, services etc.).
We believe that controllers should only be responsible for handling requests and returning a response. All remaining business logic should be placed in services.
Creating Services
It is recommended to use the CLI to create services:
terminal
entropy make service user
Service is just an injectable class with methods responsible for processing data. A basic service may look like this:
src/users/user.service.ts
export class UserService { public getMessage(): string { return 'Hello World!'; } }