A polished, interactive demonstration of dependency injection lifetimes in ASP.NET Core. This repository provides a clear, educational example of how Transient, Scoped, and Singleton services behave differently in real applications.
- Interactive Web Interface: Beautiful landing page explaining DI concepts
- Real-time Console Logging: See exactly when instances are created
- Multiple API Endpoints: Different views to understand lifetime behaviors
- Clean Architecture: Following Microsoft's recommended patterns
- Educational Focus: Built for learning and teaching DI concepts
-
Clone the repository
git clone <repository-url> cd DependencyLifetime
-
Run the application
cd DependencyLifetimeDemo dotnet run -
Open your browser
- Navigate to http://localhost:5000 for the interactive guide
- Or visit http://localhost:5000/swagger for API documentation
- Behavior: New instance created every time it's requested
- Use Cases: Lightweight, stateless services
- Example: Utility services, calculators
- In Demo: Notice different IDs everywhere
- Behavior: One instance per HTTP request
- Use Cases: Database contexts, unit of work
- Example: Entity Framework DbContext
- In Demo: Same ID within request, different between requests
- Behavior: One instance for entire application lifetime
- Use Cases: Configuration, caches
- Example: Application settings, memory cache
- In Demo: Same ID always
⚠️ Warning: Must be thread-safe!
GET /api/OperationsShows comprehensive comparison with analysis and validation.
GET /api/Operations/simpleSimplified view with short IDs for easy comparison. Call multiple times to observe behavior.
GET /api/Operations/multipleDemonstrates multiple injections within the same request.
DependencyLifetimeDemo/
├── Controllers/
│ └── OperationsController.cs # API endpoints with clear demonstrations
├── Services/
│ ├── IOperation.cs # Lifetime marker interfaces
│ ├── Operation.cs # Implementation with observable behavior
│ └── OperationService.cs # Service demonstrating dependencies
├── Pages/
│ └── Index.html # Interactive web interface
├── Program.cs # DI registration and configuration
└── README.md # Project documentation
-
Lifetime is about registration, not implementation
- Same
Operationclass behaves differently based on registration - The container manages instance creation and disposal
- Same
-
Observable behavior through GUIDs
- Each instance gets a unique ID on creation
- Compare IDs to understand instance reuse
-
Console logging for transparency
- Watch the console to see exactly when instances are created
- Instance numbers show creation order
-
Real-world patterns
- Clean separation of interfaces
- Constructor injection throughout
- No service locator anti-pattern
- Framework: .NET 8.0
- Type: ASP.NET Core Web API
- Features: Swagger UI, Static Files, Minimal APIs
- Patterns: Dependency Injection, SOLID Principles
This demo is perfect for:
- Learning: Understanding DI lifetimes hands-on
- Teaching: Clear examples for instructors
- Reference: Clean implementation to refer back to
- Interviews: Demonstrating DI knowledge
- ✅ Interface segregation with marker interfaces
- ✅ Constructor injection only
- ✅ Clear naming conventions
- ✅ Comprehensive XML documentation
- ✅ No captive dependencies
- ✅ Thread-safe singleton implementation
This project is licensed under the MIT License - see the LICENSE file for details.
Built with ❤️ for the .NET community. Perfect for learning, teaching, and mastering dependency injection!