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); // ... }
Dependency injection system in Entropy does not support circular dependencies (services dependent on each other). It is a very bad practice so the framework should not resolve circular dependencies.

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!'; } }
Views Validation