Dependency injection and inversion of control – Server Side interview prep
261 interview-style multiple-choice questions on Dependency injection and inversion of control, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 79 key terms with the flashcards first, then test yourself.
Take the practice test · Flashcards · All study tools
What it covers
- 15.1 Dependency injection and inversion of control basics
- 15.2 Injection styles and dependency design
- 15.3 The composition root and wiring
- 15.4 Lifetimes and scopes
- 15.5 Disposal, startup validation and container behavior
- 15.6 DI anti-patterns and code smells
- 15.7 Configuration, options and abstracting the environment
- 15.8 Testing with dependency injection
- 15.9 DI across frameworks and languages
Key terms
15.1 Dependency injection and inversion of control basics
- Dependency injection
- Supplying an object's dependencies from outside
- Inversion of control
- Framework controls creation and flow
- Composition root
- Where the object graph is assembled
- Service locator
- Global registry queried for dependencies
- Hollywood principle
- "Don't call us, we'll call you"
- DI container
- Builds object graphs from registrations
- Pure DI
- Hand wiring without a container
- Seam
- A place to change behavior without editing code
- Volatile dependency
- I/O, time or randomness
- Temporal coupling
- Hidden required call order
15.2 Injection styles and dependency design
- Constructor over-injection
- Too many constructor dependencies
- Circular dependency
- Two services that depend on each other
- Leaky abstraction
- Interface exposing implementation details
- Ambient context
- Static globally accessible dependency
- Property injection
- Setting optional dependencies via setters
- Method injection
- Passing a dependency per call
- Role interface
- Small interface for one client role
- Keyed service
- Registration chosen by key
- Bastard injection
- Constructor with a local default
- Interception
- Code wrapped around calls via proxies
15.3 The composition root and wiring
- Assembly scanning
- Registering types by convention
- TryAdd
- Register only if none exists
- Open generic registration
- One registration for all `T`
- Factory registration
- Lambda that builds a service
- Conforming container
- Library tied to one container
- Source-generated container
- Compile-time DI wiring
15.4 Lifetimes and scopes
- Transient
- New instance per resolution
- Scoped
- One instance per scope, often per request
- Singleton lifetime
- One instance for the container's life
- Captive dependency
- Long-lived service holding a short-lived one
- Scope
- A unit of work's lifetime
- Scope validation
- Catches scoped lifetime misuse
- Prototype scope
- Spring's new-instance-per-request
- Scoped proxy
- Proxy resolving the current scope's bean
- DbContext pooling
- Reusing reset contexts
15.5 Disposal, startup validation and container behavior
- Disposal ownership
- Whoever creates an object disposes it
- GetRequiredService
- Resolve or throw
- ASP0000
- Warning about building a second provider
- Hosted service
- Background service run by the host
- ShutdownTimeout
- How long the host waits on stop
- Container verification
- Checking all wiring up front
15.6 DI anti-patterns and code smells
- Control freak
- Creating volatile dependencies with `new`
- Constrained construction
- Forced constructor signatures
- Static facade
- Static API over the container
- Speculative generality
- Abstractions added just in case
- Primitive dependency
- Injecting bare strings or numbers
15.7 Configuration, options and abstracting the environment
- Options pattern
- Typed settings bound from configuration
- IOptionsSnapshot
- Options recomputed per scope
- IOptionsMonitor
- Singleton options with change notifications
- ValidateOnStart
- Validate options when the host starts
- Named options
- Several instances of one options type
- TimeProvider
- .NET 8 clock and timer abstraction
- IConfigureOptions
- Configurator class with injected deps
- Feature management
- Flags checked through an injected service
- ConfigurationProperties
- Spring typed configuration binding
15.8 Testing with dependency injection
- Fake
- Working lightweight implementation for tests
- Mock
- Test double that verifies interactions
- Test double
- Any stand-in for a real dependency in tests
- ValidateOnBuild
- Container option checking registrations at startup
- Auto-mocking container
- Fills all constructor params with mocks
- Strict mock
- Throws on calls without a setup
- Sociable test
- Unit test with real collaborators
- Contract test suite
- Same tests run against fake and real
- FakeLogger
- Test logger that records entries
- MockitoBean
- Spring test bean override with a mock
- Fake HttpMessageHandler
- Canned HTTP responses for HttpClient
15.9 DI across frameworks and languages
- Qualifier
- Annotation choosing among same-type beans
- Primary bean
- Default bean for a type
- Component scanning
- Finding annotated classes as beans
- Auto-configuration back-off
- Default beans skipped if you define one
- CDI
- Jakarta Contexts and Dependency Injection
- Dagger
- Compile-time DI for Java and Kotlin
- Hilt
- Dagger setup for Android
- Koin
- Kotlin DSL DI resolved at runtime
- Multi provider
- Angular token holding a list
- dependency_overrides
- FastAPI test hook for swapping deps
- Wire
- Go compile-time DI code generator
- Fx
- Go runtime DI and lifecycle framework
- provide/inject
- Vue ancestor-to-descendant values
Sample practice questions
What is inversion of control (IoC)?
- Reversing the order in which a program's methods are called at runtime
- Letting end users take control of the server's configuration settings
- Running the program's code backwards, step by step, while debugging it
- A framework calls your code and supplies its dependencies, not the reverse
Answer: D. A framework calls your code and supplies its dependencies, not the reverse DI is one form of IoC.
What is "constructor over-injection" a symptom of?
- A bug inside the DI container itself
- A slow application startup sequence
- A class with too many responsibilities
- Too few interfaces defined in the app
Answer: C. A class with too many responsibilities Consider splitting the class or introducing a facade.
What is the composition root?
- The base class that every service in the application must inherit from
- The database's root user account, which owns every schema and table
- The one place near the entry point, like `Program.cs`, where the graph is wired
- The root folder of the repository, where the solution file normally lives
Answer: C. The one place near the entry point, like `Program.cs`, where the graph is wired Only the composition root should know about the container.
In .NET DI, what does a transient lifetime mean?
- One instance per thread that resolves it
- A new instance every time it's requested
- One instance per incoming HTTP request
- One shared instance for the whole application
Answer: B. A new instance every time it's requested Use transient for lightweight, stateless services.
Who disposes services that implement `IDisposable` in the .NET container?
- The caller, which must always call `Dispose` on every resolved service
- The container, when the scope (or root) that created them is disposed
- The garbage collector, which calls `Dispose` as soon as they're unused
- Nobody; services resolved from the container are never disposed at all
Answer: B. The container, when the scope (or root) that created them is disposed Don't dispose injected services yourself.
What is the Service Locator pattern, and why is it often called an anti-pattern?
- A pattern for discovering microservices; it's considered too insecure
- Classes fetch dependencies from a global registry, hiding what they need
- A logging pattern for tracing services; it's considered too verbose
- A DNS service for locating servers; it's considered too slow for apps
Answer: B. Classes fetch dependencies from a global registry, hiding what they need Constructor injection makes the same needs visible in the signature.
Why inject an `IClock` (or `TimeProvider`) instead of calling `DateTime.Now` directly?
- ASP.NET Core forbids calling `DateTime.Now` directly
- Injecting a clock makes the code run noticeably faster
- Tests can supply a fixed time, making logic deterministic
- `DateTime.Now` doesn't work inside registered services
Answer: C. Tests can supply a fixed time, making logic deterministic .NET 8 added `TimeProvider` for exactly this.
How does DI make unit testing easier?
- It generates the unit tests automatically
- It makes the test suite run faster on the CPU
- It removes the need for assertions in tests
- Tests can pass fakes or mocks, isolating the code under test
Answer: D. Tests can pass fakes or mocks, isolating the code under test No database or network is needed for a unit test.
Does a Spring bean with a single constructor need `@Autowired` on it?
- Yes; otherwise Spring falls back to field injection for all dependencies
- No; Spring ignores constructors entirely and only injects through setters
- Yes; without it Spring calls the constructor with null for every argument
- No; since Spring 4.3 a lone constructor is used for injection automatically
Answer: D. No; since Spring 4.3 a lone constructor is used for injection automatically The annotation is needed only to choose among several constructors.
Which injection style is generally preferred, and why?
- Service locator: it keeps constructors short by hiding dependencies away
- Method injection: every call gets fresh dependencies passed in explicitly
- Constructor injection: dependencies are explicit and objects fully built
- Property injection: it needs less code and lets dependencies be optional
Answer: C. Constructor injection: dependencies are explicit and objects fully built Property injection suits truly optional dependencies.
More Server Side
- HTTP fundamentals
- REST API design
- GraphQL
- gRPC & real-time APIs
- OAuth 2.0, OIDC & JWT
- Sessions, cookies & web app security
- C# & .NET
- Java & Spring Boot
- Node.js & Express
- Python, Django & FastAPI
- Go (Golang)
- Concurrency & multithreading
- OOP & SOLID
- Design patterns
- Logging & observability
- Data structures
- Algorithms & Big-O
- Linux & shell scripting