Config Is Infrastructure
Medium About 25 min Free, no account needed
The shipping core reads a framework configuration type directly, welding the domain to infrastructure. Invert it behind a core-owned options port, and don't just rename the framework wrapper into the core, which trips a second rule.
The brief
You've inherited a shipping module. The rule that decides what shipping costs
(cartTotal >= freeOver ? 0 : 9.99) lives in Shipping.Core.RateCalculator, exactly where a domain
decision belongs. But to read its one setting (the free-shipping threshold), the calculator reaches
straight for Shipping.Framework.AppConfiguration, a framework configuration type (a stand-in for
something like IConfiguration).
That's the leak. Configuration is cross-cutting infrastructure, not domain. By taking the
framework config type as a constructor parameter, Shipping.Core carries a using Shipping.Framework:
an outward dependency from the most stable layer in the system into a framework concern. The core can
no longer be compiled or reasoned about on its own. Behaviourally it computes the same rates.
The architecture rules flag it: Shipping.Core must depend on nothing but itself and the BCL, and
there is no port expressing the core's need. The obvious move is to wrap AppConfiguration in a tidy
little options class inside the core and call it done. Try that and you'll find it isn't that simple.
The symptom (a leaked dependency) is protecting you from a deeper one, and that wrapper trips a
different rule.
Figure out why the core depends on the framework at all, then invert that dependency so the core owns the abstraction it needs, passing every architecture rule, not just the dependency one. The behavior must stay byte-for-byte identical: same cart totals in, same rates out.
- Express the core's need as a core-owned port
IShippingOptionsand haveRateCalculatorread its threshold from that port (constructor injection). - Keep the framework config type out of the core: implement
IShippingOptionsin an adapter that lives outsideShipping.Core(e.g. aShipping.Confignamespace) and wrapsAppConfiguration. - Wire the adapter in
Solution(the composition root).
Your submission must (1) still return the same shipping rate and (2) satisfy every
architecture rule shown in the results: the port must exist, Shipping.Core must depend only on
itself, and no *Configuration role may sit in the core.
The "just wrap it in the core" move is a trap. A core-side
*Configurationwrapper that still reads the framework type leaves the core depending onShipping.Framework(the dependency rule stays red) and drops a framework role right into the domain (a second rule fires). Only an adapter outside the core, implementing a core-owned port, turns them all green.
How it is graded
You work across multiple files, and architecture rules turn the design boundaries into executable feedback.
What it practices
- Infrastructure as adapters
- Keep the ORM, clocks, caches, configuration and startup work out of the core as replaceable adapters.
- Dependency inversion
- Point dependencies at abstractions the core owns, never at outer mechanisms.
Related katas
- Move the Port Home Medium Free A
*Portabstraction is declared beside its adapter, so the core reaches outward to see its own port. Move the single port into the core; duplicating it leaves a stray copy that fails the role-placement rule. - The Layer Skip Medium Free A controller reaches past the application layer straight into infrastructure for the order total. Route the call back through the middle layer with a port the application owns; a thin pass-through that still touches infrastructure only moves the smell, it doesn't remove it.
- Caching Is an Adapter Medium Pro A quote use case memoizes prices with an infrastructure cache inside its own loop, so a storage tactic lives in the application. Move caching into a decorator on the price port, and let the composition root decide how long each cache lives.
- The Core Doesn't Know Its Plugins Medium Pro A report host switches on format names and news up every plugin, and the plugins call back into the host and into each other for the report's currency rules. Let the composition root register plugins, refuse two plugins claiming one format, and hand each plugin the host's capabilities through a context the SDK publishes.