← Back to TypeScript Course | Chapter 16: Advanced Patterns | Lesson 7 of 7

Repository Pattern की जानकारी

Repository Pattern application logic को data-access details से अलग करता है। एक repository objects store और retrieve करने के लिए एक clean interface देता है जबकि underlying storage mechanism छिपाता है।
Syntax
typescript
interface Repository<T> {
  findById(id: type): Promise<T | undefined>;
  save(item: T): Promise<void>;
  delete(id: type): Promise<void>;
}

Basic Repository

एक repository domain objects के एक collection को represent करता है और finding और adding data जैसी operations expose करता है, यह छिपाते हुए कि वह data असल में एक SQL database, एक file, या एक in-memory array में रहता है।

उदाहरण: Basic Repository

typescript
interface User {
  id: number;
  name: string;
}
class UserRepository {
  private users: User[] = [];
  add(user: User) { this.users.push(user); }
  findById(id: number) { return this.users.find((u) => u.id === id); }
}
const repo = new UserRepository();
repo.add({ id: 1, name: "Ravi" });
console.log(repo.findById(1));

Repository Interface

एक repository interface application जो expect करती है (id से find करना, save करना, delete करना) को इसके पीछे की concrete storage implementation से अलग करता है, ताकि business logic को touch किए बिना storage technology बदल सके।

उदाहरण: Repository Interface

typescript
interface User { id: number; name: string; }
interface UserRepository {
  findById(id: number): User | undefined;
  save(user: User): void;
}
class InMemoryUserRepository implements UserRepository {
  private users: User[] = [];
  findById(id: number) { return this.users.find((u) => u.id === id); }
  save(user: User) { this.users.push(user); }
}
const repo: UserRepository = new InMemoryUserRepository();
repo.save({ id: 1, name: "Ravi" });
console.log(repo.findById(1));

CRUD Operations

Repositories आमतौर पर एक domain entity के लिए create, read, update, और delete operations expose करती हैं, app के हर part को उस entity की persistence के साथ interact करने का एक single, consistent तरीका देते हुए।

उदाहरण: CRUD Operations

typescript
interface Todo { id: number; title: string; }
class TodoRepository {
  private todos: Todo[] = [];
  create(todo: Todo) { this.todos.push(todo); }
  read(id: number) { return this.todos.find((t) => t.id === id); }
  update(id: number, title: string) {
    const todo = this.read(id);
    if (todo) todo.title = title;
  }
  delete(id: number) { this.todos = this.todos.filter((t) => t.id !== id); }
}
const repo = new TodoRepository();
repo.create({ id: 1, title: "Buy milk" });
repo.update(1, "Buy bread");
console.log(repo.read(1));

Service के साथ Repository

एक service storage details जाने बिना एक repository interface पर depend कर सकती है और सिर्फ business rules पर focus कर सकती है — यह सिर्फ repository से data मांगती है और इसके mechanics handle करने पर भरोसा करती है।

उदाहरण: Repository with Service

typescript
interface User { id: number; name: string; }
interface UserRepository {
  findById(id: number): User | undefined;
}
class UserService {
  constructor(private repo: UserRepository) {}
  getName(id: number): string {
    return this.repo.findById(id)?.name ?? "Unknown";
  }
}
class FakeRepo implements UserRepository {
  findById(id: number) { return { id, name: "Ravi" }; }
}
console.log(new UserService(new FakeRepo()).getName(1));

Repositories के साथ Testing

Repository abstractions testing को कहीं ज़्यादा आसान बनाते हैं क्योंकि एक fake या in-memory implementation unit tests में एक असली database की जगह ले सकती है, वही code paths exercise करते हुए slow, flaky I/O से बचते हुए।

उदाहरण: Testing with Repositories

typescript
interface User { id: number; name: string; }
interface UserRepository {
  findById(id: number): User | undefined;
}
class InMemoryTestRepo implements UserRepository {
  constructor(private users: User[]) {}
  findById(id: number) { return this.users.find((u) => u.id === id); }
}
const testRepo = new InMemoryTestRepo([{ id: 1, name: "Test User" }]);
console.log(testRepo.findById(1));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Storage details को callers में leak करना, जैसे internal array expose करना, जो pattern का purpose defeat करता है।
  2. findById से User return करना जब वह exist न करे, बजाय User | undefined के।
  3. Business rules को repository के बजाय service में रखने की जगह उन्हें repository में mix करना।
चैप्टर सारांश
  • Builder, Factory, और Singleton जैसे Creational patterns TypeScript में objects कैसे बनते हैं इसे control करते हैं।
  • Observer और Strategy जैसे Behavioral patterns objects कैसे communicate करते हैं और behavior कैसे swap होता है उसे organize करते हैं।
  • Decorator और Repository patterns structure करते हैं कि behavior कैसे जोड़ा जाता है और data access logic से कैसे अलग होता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.