Practice · 1 of 4
Restore the Seam
OrderService below is untestable — it news up its storage directly,
so no fake can stand in:
``csharp
public sealed class OrderService // BROKEN — do not ship this
{
private readonly Dictionary<int, int> _d = new(); // hard-wired storage
public int Bump(int id) { ... }
}
`
Refactor the *shape* the tests can see:
1. IStore { int Get(int id); void Put(int id, int v); } (inside Solution)
2. Solution.Service takes an IStore via constructor.
3. Service.Bump(id) adds Delta (a const int = 5) to the stored value
and returns the new value.
4. Provide two adapters: DictStore (wraps a caller-supplied dictionary)
and DoubleStore (its own private dictionary) — proving any
implementation fits the same port.
The tests inject each store in turn — the dependency points at a port, not
an implementation.
`csharp
public static class Solution
{
public interface IStore { int Get(int id); void Put(int id, int v); }
public sealed class DictStore : IStore; // wraps a supplied Dictionary<int,int>
public sealed class DoubleStore : IStore; // independent private storage
public sealed class Service // ctor(IStore); int Bump(int id)
}
``
Back to lesson: Practice: Architecture Practice: Seams, Batches, Boundaries