Dependency Injection

noun

say it: “dih-PEN-den-see in-JEK-shun” /dɪˈpɛndənsi ɪnˈdʒɛkʃən/

Handing a class the objects it needs from outside instead of letting it create them itself.

What is Dependency Injection?

Dependency injection means giving a class the things it needs (usually through its constructor) instead of the class creating them itself. It makes code easier to test and to change.

// The caller decides which sender to use
var service = new OrderService(new FakeEmailSender());

In a sentence

  • “With dependency injection, tests give OrderService a fake email sender.”

Level: Freelancer · More OOP terms at this level

Updated

In other subjects