Dependency Injection
39 bites tagged Dependency Injection — interview questions with model answers, and 60-second explainers.
Unit test a ViewModel with a mocked NetworkService
Inject the protocol, supply a mock returning canned data, assert published state transitions. dependency injection and test isolation. hitting the real network or testing the concrete service instead of the ViewModel.
Provider package versus raw InheritedWidget
It removes boilerplate, adds lifecycle disposal and scoped reads, and exposes select for granular rebuilds. why Provider wraps InheritedWidget. thinking Provider is unrelated to InheritedWidget or is its own state engine.
Describe Hilt's component hierarchy and scope injection rules
Tests whether you understand Hilt's generated component tree and scoping rules. A strong answer lists the hierarchy, explains parent-outlives-child semantics, and states that ActivityScoped-into-Singleton injection fails at compile time.
Functional difference between @Binds and @Provides in Hilt
Tests if you understand Hilt code generation tradeoffs. @Binds wires an existing injectable implementation to an interface with less overhead; @Provides constructs instances manually.
When would you provide an Angular service at the component level?
Tests Angular DI scoping beyond singletons. A strong answer picks stateful reusable widgets, explains providers create one instance per subtree, and notes cleanup on destroy. Red flag: claiming this is for performance without mentioning state isolation.
How to create a singleton Angular auth service
Tests Angular dependency injection and singleton scope. Strong answer: use the Injectable decorator with providedIn set to root for an application-wide singleton, then inject it into components.
Why is DI central to Angular architecture versus Vue's module system?
Tests architectural grasp of inversion of control. Angular DI enables hierarchical injectors, tree-shakable providers, and runtime substitution without direct imports unlike Vue modules. Red flag: calling DI mere convenience or claiming Vue matches Angular DI.
Use startup events to initialize a database pool and inject it
This tests FastAPI lifespan hooks and dependency injection for shared state. A strong answer creates the pool in an async startup handler, stores it on app.state, and accesses it via a dependency in routes. A red flag is creating a fresh pool per request.
How do router-level and app-level dependencies affect dependency override testing?
This tests FastAPI dependency hierarchy and test isolation. A strong answer states overrides are global to the app, so router-specific deps need scoped fixtures to prevent cross-test leaks. A red flag is claiming APIRouter has its own override registry.
How do you test a FastAPI endpoint without real tokens?
Tests whether you know FastAPI's dependency override mechanism to isolate business logic from auth. A great answer describes using app.dependency_overrides to swap the auth Depends for a mock returning a fake user, then cleaning up after the test.
Explain FastAPI dependency overrides with an in-memory SQLite test example
This tests FastAPI's hook for swapping dependencies cleanly in tests. A strong answer names app.dependency_overrides, defines a test-only in-memory SQLite session, and handles teardown. A red flag is patching globals or mocking ORM instead of dependency.
Implement RBAC in FastAPI with a JWT role dependency
Build a dependency that decodes the JWT, checks the role, raises 403 if not admin, and inject via Depends. FastAPI dependency composition for JWT role validation. parsing headers inside route not using dependencies.
How do you protect a FastAPI endpoint using Depends and OAuth2PasswordBearer?
OAuth2PasswordBearer sets the token URL, Depends injects it into the endpoint, and FastAPI validates the Bearer header. your grasp of FastAPI dependency injection for security.
How do you use FastAPI dependency injection for database sessions?
Build a generator dependency that yields a session and closes it after; inject via Depends(get_db). FastAPI Depends() for session lifecycle and testability.
Describe SQLAlchemy setup in FastAPI from database config to endpoint
Tests FastAPI dependency injection and SQLAlchemy session lifecycle. Good answers cover: engine with pooling, declarative models, a yield-based session dependency, and endpoint queries. Red flag: engine per request or global session shared everywhere.
Override a FastAPI dependency at the APIRouter level
Tests FastAPI DI scoping limits. Answer: APIRouter has no dependency_overrides; create a sub-app, apply overrides, mount it. Red flag: Claiming router-level overrides exist or mutating global app state.
How do you manage service lifecycle with FastAPI Depends versus formal DI?
Tests scaling FastAPI DI beyond routes. Answer: use Depends(yield) for request-scoped DB sessions; use a formal container for deep singleton service graphs and lifespan wiring; hybrid is best. Red flag: using Depends for everything and ignoring testability.
How to apply a dependency to only one FastAPI router?
Pass dependencies=[Depends(auth)] to APIRouter for /users, omit it for /items, include both. router-level dependency injection in FastAPI. repeating Depends() on every route or using middleware.
Explain the internal role of the Depends class
A strong answer notes it marks parameters for solver, enables recursive sub-dependencies and Annotated sharing, and feeds OpenAPI. If you treat Depends as FastAPI's core DI declaration primitive.
How does lifecycle differ for global vs path operation dependencies?
Global deps run on every request to any route; path-local deps run only for that route; expensive setup belongs in a lifespan event or cached singleton, not a dependency. Per-request scoping in FastAPI.
How would you implement a dependency requiring multi-source parameters?
Tests if you know FastAPI resolves dependency params like endpoint params. Great answers annotate each parameter with its source inside the dependency so FastAPI injects them independently. Red flag: manually parsing Request or merging values in the endpoint.
How does FastAPI cache dependencies within a single request?
Tests if you know FastAPI caches a dependency after the first call in a request and reuses it across the tree. A strong answer covers default use_cache=True, request-scoped lifetime, and disabling it.
How do you use a Python class as a FastAPI dependency?
It tests whether you understand FastAPI DI beyond functions and when stateful encapsulation wins. Explain that Depends takes callable classes, centralizing setup and shared state in __init__. Red flag: claiming classes are pure syntactic sugar.
How would you override a FastAPI dependency during testing?
Tests your grasp of FastAPI's dependency override mechanism. A strong answer mentions app.dependency_overrides, notes that sub-dependencies are bypassed, and stresses clearing overrides after each test.
Get Dependency Injection bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.