Ask for the Interface
Easy About 15 min Free, no account needed
An order notifier news up its own SmtpEmailer and types the field as the concrete class, even though an IMessenger abstraction sits right next to it. Make the notifier ask for the interface in its constructor. Typing the field as IMessenger while still newing the concrete inside is not the fix.
The brief
Notifications.OrderNotifier confirms orders by email. It builds its own collaborator, new SmtpEmailer(), and types the field as the concrete class. An IMessenger abstraction exists in
the same folder, and SmtpEmailer even implements it, but the notifier never asks for it. The
result: swapping the emailer for an SMS sender, or for a fake in a test, means editing the
notifier. The class that decides when to notify has welded itself to how the message travels.
The reflex fix is to retype the field:
private readonly IMessenger _messenger = new SmtpEmailer();The field now looks abstract, but the notifier still constructs the concrete, so it still
references SmtpEmailer and the "no concrete" rule stays red. Programming to an interface is not
about the declared type of a variable. It is about who chooses the implementation. As long as the
new lives inside the notifier, the notifier is doing the choosing.
Give the choice to the caller. Have OrderNotifier take an IMessenger through its constructor,
hold it in a readonly field, and use it. The composition root (the Solution class here) is the
one place allowed to know concrete types, so that is where new SmtpEmailer() belongs. The
confirmation text must stay identical.
Your submission must (1) still pass the behavior tests (same confirmation strings) and
(2) satisfy every rule shown in the results: OrderNotifier references the IMessenger
abstraction, references no SmtpEmailer at all, keeps its fields readonly, and stays sealed. The
retyped-field trap clears the abstraction rule and leaves the concrete rule failing; only
constructor injection turns both green.
Depend on abstractions, and let someone else pick the concrete. A class that constructs its own dependencies has quietly taken on a second job: deciding the shape of the system around it.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
Related guides
What it practices
- Dependency injection
- Ask for collaborators through constructors instead of newing them up or locating them.
Related katas
- Null Object: No Branching Medium Free A cart guards a nullable discount with a null check at every use. Introduce a NoDiscount Null Object so the cart never branches on absence, but do not let the cart hard-code that default. The obvious 'default it inside the cart' move re-couples the cart to a concrete; the default belongs to the composition root.
- Retire the Service Locator Medium Free A quote calculator fetches its rate table from a global ServiceLocator, so its one real dependency is invisible in every signature. Replace the locator with constructor injection. Caching the located table into a readonly field keeps the calculator quietly coupled to the global and leaves the key rule red.
- Compose the Fees Hard Free Account fee variation is modeled by subclassing: PremiumAccount and StudentAccount each override MonthlyFee, so the base can never be sealed and every new flavor is a new class. Flatten the hierarchy into one sealed Account composed with an injected IFeePolicy. Extracting the policy but hard-coding a default inside the account trips the composition-root rule.
- Fakes Don't Ship Easy Pro A test stub lives in the core and the checkout quietly falls back to it when nobody injects a gateway. Evict the stub to the testing namespace and make the dependency mandatory; a convenient default is a production code path.