C# & .NET 8 Architect Mastery

Dependency Injection (DI): Lifetime management and Captive Dependencies

1 Views Updated 5/4/2026

Mastering DI Lifetimes

Dependency Injection is the backbone of modern .NET. But if you don't understand Lifetimes, you will create subtle memory leaks and thread-safety bugs that are almost impossible to find.

1. The Three Lifetimes

  • Transient: A new instance every time it's requested. Good for lightweight, stateless services.
  • Scoped: One instance per HTTP request. Mandatory for Entity Framework DbContexts.
  • Singleton: One instance for the life of the app. Good for caches and configs.

2. Captive Dependencies

This is a senior-level mistake. A Captive Dependency occurs when a 'Longer-lived' service holds onto a 'Shorter-lived' service. Example: A **Singleton** service takes a **Scoped** service in its constructor. The Scoped service is now 'Captured' and will live forever. If that Scoped service is a DbContext, you will eventually have a database connection leak that crashes production.

4. Interview Mastery

Q: "How do you solve a Captive Dependency if you truly need a Scoped service in a Singleton?"

Architect Answer: "You use the **IServiceScopeFactory**. Instead of injecting the Scoped service directly, you inject the factory. Inside the Singleton's method, you call `factory.CreateScope()`. You can then resolve the Scoped service from that temporary scope using a `using` block. This ensures the Scoped service is correctly disposed of as soon as your method is finished, while the Singleton continues to live on."

C# & .NET 8 Architect Mastery
1. Memory Management & Performance
The CLR Deep Dive: Stack, Heap, and Garbage Collection (GC) Value Types vs Reference Types: Structs, Records, and Classes Span<T> and Memory<T>: Zero-copy high-performance code Benchmarking with Benchmark.DotNet: Measuring nano-seconds
2. Advanced Asynchronous Programming
Async/Await Internals: The State Machine and TaskContext ValueTask vs Task: Avoiding allocation in hot paths Task.WhenAll vs Parallel.ForEachAsync: Concurrency at scale Thread Safety & Multi-threading: Locks, Semaphores, and Interlocked
3. Modern C# 12+ Features
Primary Constructors & Collection Expressions Pattern Matching: Switch expressions and Recursive patterns Required Members & Init-Only properties Native AOT (Ahead of Time): Deployment for serverless/edge
4. Enterprise Design Patterns in .NET
Dependency Injection (DI): Lifetime management and Captive Dependencies The Options Pattern: Type-safe configuration management The Factory & Builder Patterns in Modern .NET Middleware Architecture: Building custom ASP.NET Core pipelines
5. Dynamic Programming & Reflection
Reflection & Attributes: Building custom frameworks Expression Trees: Building dynamic LINQ queries Source Generators: Compile-time code generation for speed Dynamic Types & ExpandoObject: When and when not to use them
6. Testing & Quality Architecture
Unit Testing Patterns: xUnit, Moq, and FluentAssertions Integration Testing with WebApplicationFactory Mutation Testing: Testing the quality of your tests TDD (Test Driven Development) for Senior Architects
7. Modern Web API Architectures
Minimal APIs vs Controllers: Choice of architecture Rate Limiting & Throttling in ASP.NET Core Versioning Strategies: URL vs Header vs Media Type Real-time Web with SignalR and WebSockets
8. FAANG .NET Architect Interview
Case Study: Designing a High-Throughput Payment Gateway in .NET Case Study: Solving Memory Leaks and CPU Spikes in Production