Lazy<T> and singletons done right
The static Instance property, the hand-rolled double-checked lock and the Lazy<Task
The static Instance property, the hand-rolled double-checked lock and the Lazy<Task
An if (Simulation) in every method of a device driver looks harmless, until real and fake behaviour are tangled together in one class. Choose the implementation once at the composition root, keep fakes in their own assembly, and keep test code out of what you ship.
The same switch over a shipping method in five places, each case a small algorithm, and every new option means touching all of them. How to turn it into strategies, resolve them with a dictionary or .NET 8 keyed services, and when a plain switch is still the better choice.
Writing an Add extension for every service gets old, and scanning the assembly feels like magic until it registers the wrong thing. The options for automatic registration — reflection, Scrutor, graph walking, source generators — and the trade-offs that decide which one I use.
Singleton, scoped, transient, captive dependencies, and a Program.cs with 150 AddScoped lines nobody understands. How I structure registrations per feature, and how to let the container catch lifetime bugs before production does.
A large ASP.NET Core monolith needed five to seven seconds to start, and registering only the endpoints a request needs made it three to four times faster. Where startup time really goes, why the endpoint isn't the real target, and the Lazy