Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • NestJS Documentation21টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Modules
  • Controllers
  • Providers & DI
  • Pipes
  • Guards
  • Interceptors
  • Exception Handling
  • Middleware
  • Database
  • Authentication
  • Config & Env
  • File Upload
  • WebSockets
  • Microservices
  • Swagger
  • Testing
  • Best Practices
  • Project
  • Interview
  1. Docs
  2. NestJS
  3. Providers এবং Dependency Injection
NestJSIntermediate

Providers এবং Dependency Injection

@Injectable, constructor injection, custom providers, scopes — NestJS DI বাংলায়।

12টি অংশবাংলা ভাষায়

Provider হলো যেকোনো class যা @Injectable() decorator ব্যবহার করে। Dependency Injection (DI) হলো এই providers-কে যেখানে দরকার সেখানে auto-প্রদান করা।

@Injectable ও Service

src/users/users.service.tstypescript
typescript
import { Injectable, NotFoundException } from "@nestjs/common";

export interface User {
  id: number;
  name: string;
  email: string;
}

@Injectable()
export class UsersService {
  private users: User[] = [
    { id: 1, name: "রহিম", email: "rahim@example.com" },
    { id: 2, name: "করিম", email: "karim@example.com" },
  ];

  findAll(): User[] {
    return this.users;
  }

  findOne(id: number): User {
    const user = this.users.find((u) => u.id === id);
    if (!user) throw new NotFoundException("User পাওয়া যায়নি");
    return user;
  }

  create(data: Omit<User, "id">): User {
    const user = { id: Date.now(), ...data };
    this.users.push(user);
    return user;
  }

  update(id: number, data: Partial<User>): User {
    const user = this.findOne(id);
    Object.assign(user, data);
    return user;
  }

  remove(id: number): void {
    this.findOne(id);
    this.users = this.users.filter((u) => u.id !== id);
  }
}

Constructor DI — Service ব্যবহার

users.controller.tstypescript
typescript
import { Controller } from "@nestjs/common";
import { UsersService } from "./users.service";

@Controller("users")
export class UsersController {
  // DI — constructor-এ inject
  constructor(private readonly usersService: UsersService) {}

  @Get()
  findAll() {
    return this.usersService.findAll();
  }
}

constructor(private readonly usersService: UsersService) — TypeScript এর shorthand। NestJS auto-detect করে UsersService created instance inject করে।

Provider Scopes

ScopeLifecycleকখন
SINGLETON (default)পুরো app-এ এক instanceসাধারণত এটাই দরকার
REQUESTপ্রতি request-এ নতুন instanceuser-specific state দরকার হলে
TRANSIENTপ্রতি injection-এ নতুন instanceতারপরই সম্প্রতি দরকার হলে
import { Injectable, Scope } from "@nestjs/common";

@Injectable({ scope: Scope.REQUEST })
export class RequestScopedService {
  private requestId = Math.random();
  getRequestId() { return this.requestId; }
}

Custom Provider (Factory)

import { Module } from "@nestjs/common";

@Module({
  providers: [
    {
      provide: "CONFIG",
      useFactory: () => ({
        port: process.env.PORT || 3000,
        env: process.env.NODE_ENV,
      }),
    },
  ],
})
export class AppModule {}

// ব্যবহার
constructor(
  @Inject("CONFIG") private config: { port: string; env: string },
) {}
পরিবেশ type

Providers-এ business logic রাখুন, controller-এ শুধু request/response — এতে code testable ও maintainable হয়।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরNestJS ControllersপরেরNestJS Pipes & Validation
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com