SOLID and dependency boundaries
SOLID and dependency boundaries
SOLID is a set of design heuristics for making changes safely. Start with a concrete reason to change and protect existing behavior before introducing a new abstraction.
Cohesion describes how closely a component's responsibilities belong together; coupling describes how strongly it depends on other components. Single responsibility (SRP) groups behavior that changes for the same reason. Open/closed (OCP) makes a known variation replaceable without repeatedly modifying stable policy. Liskov substitution (LSP) requires implementations to preserve the observable contract, including preconditions, results and failure behavior. Interface segregation (ISP) gives clients the capabilities they need rather than a broad collection of unrelated operations. Dependency inversion (DIP) puts the contract near the policy that needs it, with details implementing that contract. Constructor injection is one way of supplying an implementation, not a requirement to use a DI container. A small pure function may be the best seam. Avoid introducing interfaces for every class, resolving dependencies from a global service locator, or sharing mutable static state. A characterization test records current behavior before refactoring; it can also capture a bug, so explicitly decide which behaviors are intended. Here the shipping contract is nonnegative weight, a base fee plus a per-kilogram fee, and no rounding until presentation. All examples are fictional, and values are decimal currency units, not current prices.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Offline models and multi-project labs are explicitly distinguished.
Preserve behavior before extracting a policy
Compare the old formula with the extracted policy at three boundaries. The test stops on a mismatch.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;foreach (decimal kg in new[] { 0m, 1m, 5m }) { decimal before = OldPrice(kg); decimal after = new ShippingQuote(new StandardRate()).Price(kg); if (before != after) throw new Exception("Regression"); Console.WriteLine($"{kg}: {after}");}static decimal OldPrice(decimal kg) => kg < 0 ? throw new ArgumentOutOfRangeException(nameof(kg)) : 3m + 2m * kg;interface IRate { decimal Fee(decimal kg); }sealed class StandardRate : IRate { public decimal Fee(decimal kg) => 3m + 2m * kg; }sealed class ShippingQuote(IRate rate) { public decimal Price(decimal kg) => kg < 0 ? throw new ArgumentOutOfRangeException(nameof(kg)) : rate.Fee(kg);}Expected result (not executed):
0: 31: 55: 13Input validation remains in the public quote operation. Rate implementations assume validated weight; keep that precondition documented. The same test cases should be run on both versions, including invalid input.
Substitution needs a contract, not just an interface
Test standard and pickup policies against the common promise that a quote is nonnegative for accepted weight.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;IRate[] policies = [new Standard(), new Pickup()];foreach (var policy in policies) { foreach (decimal kg in new[] { 0m, 2m }) { var fee = policy.Fee(kg); if (fee < 0) throw new Exception("Contract broken"); Console.WriteLine(fee); }}interface IRate { decimal Fee(decimal nonnegativeKg); }sealed class Standard : IRate { public decimal Fee(decimal nonnegativeKg) => 3m + 2m * nonnegativeKg; }sealed class Pickup : IRate { public decimal Fee(decimal nonnegativeKg) => 0m; }Expected result (not executed):
3700Pickup deliberately returns zero without restricting valid weight. A replacement that throws for an otherwise valid zero-weight input would violate the stated contract. These finite checks are not a proof over all decimal inputs; agree on realistic upper bounds in production.
Inject only the capability the client uses
A read-only shipping quote does not need warehouse mutation methods. The fake proves the seam without any container or network.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;var quote = new Quote(new FixedRate(8m));Console.WriteLine(quote.Total(20m));interface IReadRate { decimal Current(); }sealed class FixedRate(decimal value) : IReadRate { public decimal Current() => value; }sealed class Quote(IReadRate rates) { public decimal Total(decimal subtotal) => subtotal + rates.Current();}Expected result (not executed):
28The quote depends only on a read operation. A separate administration interface can own changes. If the rate never varies, a decimal constructor argument may be enough.
Apply the concept
A warehouse adds an express rate and a separate administration screen. Refactor the first example, preserve old quotes, and describe an LSP test.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SOLID and dependency boundaries — knowledge check
Apply the lesson concepts to the following scenarios.
What motivates SRP in the shipping example?
Which test reveals a substitution problem?
Where should a policy-owned abstraction live?
What is the best first step before a behavior-preserving refactor?
When is a direct decimal parameter preferable to an interface?