🚀 Today’s Post | Interview Question #36 Dependency Injection Lifecycles in .NET 🧐 Here’s your quick guide to DI in .NET Core: 🔹 Singleton ♾️ ✅ One instance for the entire application ✅ Perfect for stateless, shared services (e.g., Logger) ❌ Dangerous for storing state! 🔹 Scoped 🔄 ✅ One instance per user request (e.g., in a web app) ✅ Ideal for Entity Framework DbContexts ❌ Not thread-safe outside a request 🔹 Transient ⚡ ✅ A new instance is created every time it’s requested ✅ Great for lightweight, stateless services ❌ Can cause memory overhead if overused 💡 Quick Recap: Singleton = Shared, global state Scoped = Per user session/request Transient = Always new, never shared Mastering these lifecycles will level up your architecture and prevent nasty bugs. 🚀 👉 Which lifecycle do you use the most in your projects? 🔑 Hashtags #DotNet #DotNetCore #DependencyInjection #SoftwareArchitecture #Programming #BackendDevelopment #WebDevelopment #Microservices #Coding #SoftwareDevelopment #Tech #CodeNewbie #LearnToCode
How singleton became stateless ?
I love this method. In Fastify.js for example, a good plugin is Awilix! I applied and I recommend for all.
Nice
Thanks for sharing
for older as me .net dev, Scope = Session (ASPX), Singleton = Application (Global.asax)
You should use singleton to store state if needed, singleton are for sharing around...
You mentioned that Singleton is “dangerous for storing state,” but in the recap you wrote “Singleton = Shared, global state.”
even for the loggers singletons are bad, unless unit tests and integration tests are not on the table. It's ok-ish for small projects, otherwise the future pain is not worth the momentary relief.
Technical Lead @ سابتكو | Team Management, Software Architecture
1mo👌👌