Inversion of control and dependency injection, service registration and resolution, constructor, property, method and factory injection, alternative containers, DI in libraries and tests.
-
What is the composition root, and why should only the composition root resolve services from the container?
Beginner
Theory
DI Core Concepts
-
Why is calling services.BuildServiceProvider() inside Program.cs a problem, and what do you do instead?
Junior
Theory
DI Core Concepts
-
What does ValidateOnBuild check, and what can it not catch?
Intermediate
Theory
DI Core Concepts
-
What is the difference between inversion of control, dependency injection and the Dependency Inversion Principle? Why do we use DI at all, and what does it look like in an ASP.NET Core app?
Defines inversion of control, dependency injection and the Dependency Inversion Principle, how they relate, why DI is used, and how the built-in ASP.NET Core container applies it.
Beginner
Theory
DI Core Concepts
Β· π 3 follow-ups
-
How do you register services with the built-in .NET container? Explain AddSingleton, AddScoped and AddTransient, the TryAdd* methods and Replace, and what happens when you register several implementations of the same interface and inject IEnumerable<T>.
Covers registering services in IServiceCollection: lifetime methods, TryAdd, TryAddEnumerable and Replace, last-registration-wins resolution and injecting all implementations with IEnumerable<T>.
Junior
Theory
DI Core Concepts
Β· π 3 follow-ups
-
How does the built-in .NET container actually resolve a service? Explain how it chooses a constructor, what call sites are, how ActivatorUtilities differs from GetService, and how the container makes resolution fast after the first call.
Explains resolution in Microsoft.Extensions.DependencyInjection: constructor selection rules, the call-site tree and cache, runtime and compiled resolvers, ActivatorUtilities vs GetService, and what errors you get when resolution fails.
Intermediate
Theory
DI Core Concepts
Β· π 3 follow-ups
-
What are constructor injection, property injection and method injection? Why does the built-in .NET container only support constructor injection, and where do you still see method injection in ASP.NET Core?
Compares constructor, property and method injection, explains why Microsoft.Extensions.DependencyInjection supports only constructors, and shows where ASP.NET Core injects into methods and properties (endpoint parameters, [FromServices], InvokeAsync, @inject, [Inject]).
Junior
Theory
DI Core Concepts
Β· π 3 follow-ups
-
Sometimes a service needs an object it can't get from the container at construction time: a dependency that is expensive and rarely used, a new instance per operation, or one that needs a run-time value such as a tenant or file name. Show how you would solve this with factory delegates, Func<T>, Lazy<T> and ActivatorUtilities, and explain the trade-offs.
Shows factory injection in Microsoft.Extensions.DependencyInjection: factory registrations, explicit Func<T> and Lazy<T> registrations, typed factory interfaces, ActivatorUtilities.CreateFactory for run-time arguments, and disposal and lifetime pitfalls.
Mid-Level
Coding
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
When is the built-in Microsoft.Extensions.DependencyInjection container not enough? Compare it with Autofac, Lamar and Scrutor: what features does the built-in container deliberately leave out, and when would you bring in another container or library?
Compares the built-in .NET container with Autofac, Lamar and Scrutor: missing features (property injection, scanning, decorators, child containers, implicit Func/Lazy), how third-party containers plug in with UseServiceProviderFactory, and when switching is justified.
Intermediate
Theory
DI Core Concepts
Β· π 3 follow-ups
-
You are writing a NuGet library that other teams will use in their ASP.NET Core apps. How should it integrate with dependency injection? Explain AddMyLibrary() extension methods, options, TryAdd, builder objects, and how you avoid tying consumers to one container.
Covers DI design for reusable .NET libraries: IServiceCollection extension methods, options and validation, TryAdd and TryAddEnumerable for overridable defaults, builder objects for chained configuration, depending only on abstractions packages, and lifetime choices.
Senior
Theory
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
How do you use dependency injection in tests? Show how you replace services in an ASP.NET Core integration test with WebApplicationFactory and ConfigureTestServices, when you use fakes versus mocks, and why unit tests usually shouldn't need a container at all.
Shows testing with DI: constructor-injected unit tests without a container, WebApplicationFactory with ConfigureTestServices to replace services, RemoveAll and Replace, fakes vs mocks, FakeTimeProvider, and testing the composition root with ValidateOnBuild.
Mid-Level
Coding
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
Code review: an OrderService constructor takes 12 injected dependencies (repositories, email, SMS, PDF, tax, discount, audit, cache, logger and more), and its methods also new up a TaxCalculator and an HttpClient. What problems do you see, ranked by severity, and how would you refactor it?
Reviews a class with constructor over-injection and hidden new-ed dependencies: SRP violations, socket exhaustion from new HttpClient, untestable collaborators, and refactoring into focused handlers, domain events and facade services.
Mid-Level
CodeReview
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
You join a team with a large legacy .NET application full of static helper classes, a hand-rolled ServiceLocator.Get<T>() and singletons that read configuration directly. How would you introduce dependency injection without a big-bang rewrite? Explain the composition root, the order you would tackle things in, and how you would keep the app shippable throughout.
A real-world strategy for introducing DI into a legacy .NET app: creating a composition root, wrapping statics behind interfaces, bridging the service locator to the container, strangling it incrementally, characterisation tests, and measuring progress.
Senior
RealWorldScenario
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world