๐ DI Lifetimes in .NET Explained Like a Party You Actually Want to Attend
If youโve ever worked with .NET Dependency Injection, youโve met these three characters:
๐ DI Lifetimes in .NET Explained Like a Party You Actually Want to Attend
If youโve ever worked with .NET Dependency Injection, youโve met these three characters:
Singleton, Scoped, and Transient.
At first, they sound harmless. Friendly, even.
And then one day your app throws a mysterious ObjectDisposedException, your logs start screaming, and you realizeโฆ
You invited the wrong lifetime to the party.
Letโs fix that. No boring textbook explanations. Just vibes, analogies, and fewer production bugs.
๐ญ Meet the Three Guests

๐ Singleton โ โI live here nowโ
Only one instance for the whole application.
Created once.
Lives until the app shuts down.
Everyone shares it. Forever.
Think: the person who shows up early, takes the couch, and never leaves.
Good examples: Logger, Cache service, Configuration, HttpClient (or via a factory)
Singletons are great when something is:
Expensive to create
Thread-safe
Meant to be shared by everyone
๐งณ Scoped โ โIโm here for this request onlyโ
Created once per HTTP request.
Shared inside that request.
Destroyed when the request ends.
Think: someone who comes for this party, helps clean up, and goes home.
Good examples: DbContext, Repositories, Business services, Current user / request context
Important detail:
Scoped is not per controller. Itโs per request.
If Controller A and Controller B are part of the same request and both use the same scoped service โ they get the same instance.
๐ Transient โ โI just popped in for a secondโ
New instance every time you ask for it.
Ask twice โ you get two different objects.
No memory. No commitment.
Think: someone who shows up, says hi, disappears, and if you see them againโฆ itโs basically a new person.
Good examples: Helpers, Mappers, Formatters, Small stateless utilities
If you inject a transient service twice in the same constructor:
Surprise! They are not the same object ๐
No more confusion. No more lifetime chaos. Now grab a slice ๐ โ the partyโs just getting started.
Happy DI Pizza Party ๐
๋ฉํ๋ฐ์ดํฐ
- post_id
- 7e70ac066eec
- slug
- di-lifetimes-in-net-explained-like-a-party-you-actually-want-to-attend-7e70ac066eec
- url
- https://medium.com/@redwanniloy161293/di-lifetimes-in-net-explained-like-a-party-you-actually-want-to-attend-7e70ac066eec
- canonical_url
- https://medium.com/@redwanniloy161293/di-lifetimes-in-net-explained-like-a-party-you-actually-want-to-attend-7e70ac066eec
- author_url
- https://medium.com/@redwanniloy161293
- status
- ok
- fetched_at
- 2026-07-13 22:23:34