0% found this document useful (0 votes)
20 views1 page

Coding Concepts 3

test 3

Uploaded by

Rizwan Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views1 page

Coding Concepts 3

test 3

Uploaded by

Rizwan Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In the context of dependency injection, "scoped," "transient," and "singleton" refer to different lifetimes for the services or objects

that are
managed by the dependency injection (DI) container. Here's a brief overview of each:

1. **Transient**:
- **Lifetime**: Created each time they are requested.
- **Use Case**: For lightweight, stateless services where each operation requires a new instance. Ideal for objects that don’t hold state
between calls or need to be unique per request.
- **Example**: A service that performs a calculation or a short-lived operation.

2. **Scoped**:
- **Lifetime**: Created once per request (or per scope). In web applications, this typically means a new instance is created for each HTTP
request.
- **Use Case**: For services that need to maintain state throughout a single request but should be shared within that request. Useful for
services that need to be consistent during the lifetime of the request but should not persist across different requests.
- **Example**: A service that manages a unit of work or transaction within a request.

3. **Singleton**:
- **Lifetime**: Created once and shared throughout the application's lifetime.
- **Use Case**: For services that are expensive to create, need to be shared across multiple components, or need to maintain state for the
entire duration of the application. Singleton services are initialized once and can be reused throughout the application.
- **Example**: A configuration service that holds application settings or a logging service.

Choosing the right lifetime for your services helps in managing their state and resource usage efficiently in your application.

You might also like