Skip to content
Katabench
Try free

Own the Lease Until the Work Ends

Medium About 20 min Free, no account needed

Keep acquisition and deterministic disposal in one scope, including exception paths, while accepting any lease factory.

The brief

LeaseRunner acquires a work lease and processes one value, but never closes the lease. Successful output is correct, so a string comparison alone cannot reveal the leaked ownership. Closing only after the call would still leak when processing throws.

Introduce IWorkLease : IDisposable with string Apply(int value), ILeaseFactory with IWorkLease Acquire(), and a LocalLeaseFactory whose Acquire returns a new LocalLease. LeaseRunner must accept a factory in its constructor, acquire exactly one fresh lease for each Run, call its Apply exactly once, and dispose that lease exactly once before returning or propagating a processing exception. The runner owns leases it acquires; it does not dispose the factory. Do not cache a lease across invocations.

Keep LeaseRunner sealed, hold its factory readonly, and remove its dependency on LocalLease without adding one on LocalLeaseFactory. The root (Solution) passes a LocalLeaseFactory to the runner. LocalLease implements the lease contract. Preserve the behavior of Solution.Run(int value): nonnegative values produce handled:<value>; negative values produce failed:negative from the root's existing exception handler.

The trusted seam supplies counting leases that succeed or throw. It invokes the same runner twice and checks acquisition, processing, and disposal counts, so constructor injection without correct ownership remains incomplete.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

What it practices

Resource lifetime
Acquire and dispose deterministically, including on exception paths.

Related katas