What is a Dependency
A dependency is an object a class needs to do its job. When a class creates its own dependencies with new, it's wired to them for good, and hard to test.
Apprentice · C# · OOP · Updated
After this lesson you can
- Identify a class's dependencies, and explain the problems of creating them inside the class
Key takeaways
- A dependency is an object a class needs to do its job, such as the
ClockthatGreeterneeds. - A class that creates its own dependencies with
newis hard-wired to them: to change one, you have to change the class itself. - A
Greeterthat makes its ownnew Clock()can't be tested reliably, because its greeting depends on when the test runs. - Dependency injection hands a class its dependencies from outside, so whoever creates the class decides which ones it gets.
Classes rarely work alone. A ProductDao needs a connection string. A controller needs a DAO. An order needs its menu items. When a class needs another object to do its job, that object is a dependency: the class depends on it.
A greeter that depends on a clock
Here's a Greeter that says good morning or good afternoon, depending on the time. It gets the time from a Clock:
Greeter greeter = new Greeter();
Console.WriteLine(greeter.Greet("Ada"));
public class Clock
{
public virtual int Hour => DateTime.Now.Hour;
}
public class Greeter
{
private readonly Clock clock = new Clock();
public string Greet(string name)
{
return clock.Hour < 12 ? quot;Good morning, {name}!" : quot;Good afternoon, {name}!";
}
}
Clock is a dependency of Greeter. And Greeter creates its own: new Clock(), right there in the class.
The trouble with new inside a class
It works. But run it in the morning and it says Good morning; run it after lunch and it says Good afternoon. Now try to test it:
- A test that expects Good morning, Ada! passes before noon and fails after. A test whose result depends on when you run it can't be trusted.
- To test the afternoon greeting, you'd have to wait until the afternoon.
- There's no way to hand
Greetera different clock, because it builds its own.
The same trouble shows up everywhere a class creates what it needs:
| The class creates | So it's stuck with | And testing it means |
|---|---|---|
new Clock() |
The real time | Waiting for the right time of day |
new ProductDao("Server=localhost;Database=Northwind;...") |
That database, on that server | Having that database, with the right rows in it |
new EmailSender() |
Sending real email | Sending real email to someone, every test run |
A class that creates its dependencies is hard-wired to them: to change one, you have to change the class itself.
The fix, in one sentence
Don't let the class create its dependencies. Hand them in from outside, so whoever creates the class decides which clock, which database, which email sender it gets. That's dependency injection, and the next lesson shows how.
Do it: hand the greeter its hour
The greeter now has a third greeting, for the evening. But it still reads the hour from its own new Clock(), so nobody can check the evening greeting without waiting for the evening. Change Greet so the hour is handed in from outside.
Greet gets two values:
name: the person's name, astring("Ada").hour: the hour of the day, anintfrom0to23(15is 3 p.m.).
The greeting depends on the hour:
hour |
Greet("Ada", hour) returns |
|---|---|
| 0 to 11 | "Good morning, Ada!" |
| 12 to 17 | "Good afternoon, Ada!" |
| 18 to 23 | "Good evening, Ada!" |
For example, at 3 p.m.:
Greeter greeter = new Greeter();
string greeting = greeter.Greet("Ada", 15);
15 is in 12 to 17, so greeting is "Good afternoon, Ada!".
Your job: change the method Greet(string name, int hour) in the Greeter class so the greeting comes from the hour it's handed, not from the clock.
- Use
houreverywhere the starter usesclock.Hour. - Return the greeting from the table, with the person's name in it.
- Noon (
12) is afternoon, and 6 p.m. (18) is evening. Greeterno longer needs itsClockfield: take it out.
Run the starter first: which of its tests pass depends on the time of day you run it. That's the trouble this lesson is about.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
1 question to try on the lesson page.
So what
Every new inside a class for something it depends on is a decision made too early. Spot those, and you've found the places where your code is hard to test and hard to change.