Suggest an editImprove this articleRefine the answer for “What does the "lifetime" of dependencies (singleton, scoped, transient) mean?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Dependency "lifetime"** is a concept from **Dependency Injection** systems that defines how long an object created by the dependency container will exist and when it will be recreated. **Key point:** singleton is one for the whole application, scoped is one per context (for example, a request), transient is a new one every time.Shown above the full answer for quick recall.Answer (EN)Image"**Lifetime**" of dependencies (dependency lifetime) is a concept from **Dependency Injection** systems that defines: > *how long an object created by the dependency container will exist and when it will be recreated.* In other words, **lifetime** manages the *lifecycle of dependency instances* in an application. --- ### Main lifetime types #### 1. **Singleton (one for the whole application)** The container creates **a single instance** of the object and uses it everywhere the dependency is needed. ```java @Singleton class ConfigService { ... } ``` **How it works:** - created once at application startup, - used by all components, - destroyed when the program shuts down. **Fits:** - configuration, loggers, caching, constant services. **Does not fit:** - components that depend on user data, a request, or state. **Idea:** "one for everyone, forever". --- #### 2. **Scoped (one per context, for example a request/session)** The container creates **a new instance for each "context"**, for example an HTTP request, a user session, or a transaction. ```csharp services.AddScoped<IUserService, UserService>(); ``` **How it works:** - the same object is used within one context, - the container creates a new instance for a new request. **Fits:** - working with request data, transactions, database contexts. **Does not fit:** - global services that should be shared across everyone. **Idea:** "one object per request". --- #### 3. **Transient (new on every use)** The container creates **a new dependency instance every time** it is requested. ```csharp services.AddTransient<IFormatter, HtmlFormatter>(); ``` **How it works:** - every request to the container returns a new object, - no caching, no reuse. **Fits:** - lightweight, short-lived objects, - utilities, formatters, event handlers. **Does not fit:** - heavy or slow components that are created often. **Idea:** "a clean instance every time". --- ### An illustrative example (C# / .NET) ```csharp services.AddSingleton<Logger>(); services.AddScoped<UserRepository>(); services.AddTransient<NotificationService>(); ``` How this works: | Component | Created | How long it lives | |---|---|---| | **Logger** | once at startup | the whole time | | **UserRepository** | once per HTTP request | until the request ends | | **NotificationService** | new every time | only while it is used | --- ### Why this matters Incorrect lifetime management can lead to: - **memory leaks** (if a scoped object gets "glued" to a singleton), - **data desynchronization** (if a singleton holds mutable state), - **random conflicts** (if transient objects are created too often). --- **Conclusion:** > Lifetime is the "life span" of dependencies in a DI container. > It defines **when an object is created, how long it lives, and who owns it.** - **Singleton** - one for the whole application. - **Scoped** - one per context (for example, a request). - **Transient** - a new one every time.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.