https://github.com/matheusadc/dependency-injection-scopes
Study on Dependency Injection Scopes.
https://github.com/matheusadc/dependency-injection-scopes
csharp dependency-injection
Last synced: about 1 year ago
JSON representation
Study on Dependency Injection Scopes.
- Host: GitHub
- URL: https://github.com/matheusadc/dependency-injection-scopes
- Owner: MatheusADC
- Created: 2025-01-15T19:37:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-15T19:51:52.000Z (over 1 year ago)
- Last Synced: 2025-01-15T22:06:42.709Z (over 1 year ago)
- Topics: csharp, dependency-injection
- Language: C#
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#
Description
Study on Dependency Injection Scopes.
# 🎯Scopes
### 🔄 1. Singleton Scope
- Creates one single instance for the entire application lifecycle;
- All consumers use the same instance;
- Suitable for stateful services or heavy resources like databases, caches, etc.
### 🌱 2. Transient Scope
- Creates a new instance every time the dependency is requested;
- Each consumer gets a different instance;
- Ideal for stateless or short-lived services.
### 🌐 3. Scoped Scope
- Creates a new instance per request (e.g., one instance per HTTP request);
- Same instance is shared across the request but discarded after it ends;
- Useful for user-specific services in web applications.
# 🎛️Swagger

# ✅Dependency Injection Lifecycle Scopes
| **Scope** | **Instance Creation** | **Sharing** | **Use Case** |
|----------------|-------------------------------------------------|---------------------------------|----------------------------------|
| **Singleton** | Once for the entire application lifecycle | Same instance for all consumers | Logging, Cache, Configurations |
| **Transient** | A new instance every time it's requested | Different instance for each call | Stateless services, Helpers |
| **Scoped** | Once per request (e.g., per HTTP request) | Same instance within the request | User-related services (Web apps) |