शब्दकोश · प्रोग्रामिंग

Dependency Injection

Dependency injection is a technique where an object receives the other objects it depends on from outside, rather than creating them itself.

अपडेट: अक्टूबर 6, 2026

Dependency injection (DI) is a technique in which a component is given the objects it depends on, usually through its constructor, instead of creating them internally. The code that wires things together is separate from the code that uses them.

Why it matters

When a class builds its own database client or HTTP client, it is tightly coupled to it and hard to test. With DI you can pass in a real client in production and a fake in a test, or swap implementations without editing the class. Frameworks such as Spring, ASP.NET Core and Angular include DI containers that build and supply dependencies automatically, but the idea works with plain constructors and needs no framework.

Example

class Checker {
  constructor(http) { this.http = http; }
  async isUp(url) {
    const res = await this.http.get(url);
    return res.status < 400;
  }
}

// production
new Checker(realHttpClient);
// test
new Checker({ get: async () => ({ status: 200 }) });

Common mistakes

  • Treating a DI container as required and hiding dependencies behind global lookups.
  • Injecting too many dependencies, a sign the class does too much.
  • Confusing it with dependency management through a package manager.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.