Understanding Dependency Injection Lifecycles in .NET Core

View profile for Medhat ali

Technical Lead @ سابتكو | Team Management, Software Architecture

🚀 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

  • No alternative text description for this image
Medhat ali

Technical Lead @ سابتكو | Team Management, Software Architecture

1mo

👌👌

Hitesh Inti

C#.NET | ASP.NET | MVC |.NET CORE(5,8) | WEB API |SQL SERVER | JQUERY | ANGULAR(9,14) |AZURE DEVOPS(CI/CD)| NUNIT |AZURE APP SERVICES | MICRO-SERVICES | DOCKER | CLEAN ARCHITECTURE | LAYERED ARCHITECTURE

1mo

How singleton became stateless ?

Hugo Souza

Desenvolvedor Sênior • N8N • Next.js • Flutter • Node.js • TypeScript • SQL • Docker • Git

1mo

I love this method. In Fastify.js for example, a good plugin is Awilix! I applied and I recommend for all.

Imran Khan

Sr. Software developer at Hexaware Technologies.

1mo

Nice

Tural Aghasiyev

Senior Software Engineer |.NET | Azure | SQL

1mo

Thanks for sharing

Francesco Mantovanelli

Technical Consultant / Software Architect .NET

1mo

for older as me .net dev, Scope = Session (ASPX), Singleton = Application (Global.asax)

Faruk Feres

Technical Product Manager at Mottu

1mo

You should use singleton to store state if needed, singleton are for sharing around...

Miguel Caravantes

Software Engineer at Ski.com

1mo

You mentioned that Singleton is “dangerous for storing state,” but in the recap you wrote “Singleton = Shared, global state.”

Vladimir Gatov

Experienced .Net Architect and Software Developer

1mo

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.

See more comments

To view or add a comment, sign in

Explore content categories