Dependency Injection
39 bites tagged Dependency Injection — interview questions with model answers, and 60-second explainers.
How do you apply a dependency to an APIRouter without per-endpoint signatures?
Pass Depends() to APIRouter dependencies parameter; runs before every route in that router and shows in docs. FastAPI router-level DI. using middleware or manual decorators over the native dependencies argument.
How do you declare a function as a dependency, and why?
Tests FastAPI dependency injection basics. Answer: create a function, import Depends, and add it to path operation parameters so FastAPI injects it. Purpose: routes declare what they need instead of hard-coding shared logic.
How do you create a reusable current-user dependency in FastAPI?
Tests DRY auth with FastAPI Depends. Answer: create get_current_user that Depends on OAuth2PasswordBearer, verifies token, returns User model, inject into routes. Red flag: middleware or manual header parsing in each endpoint.
Implement an async database session dependency using yield for setup and teardown
This tests async resource lifecycle management in FastAPI. A strong answer uses async def, yields a session inside try, closes in finally, and injects with Depends. A red flag is omitting finally or using sync def for async I/O, which leaks connections.
Why does Go forbid circular dependencies, and how do you resolve them?
Cycles break incremental compilation; resolve by moving logic down, merging coupled packages, or using dependency injection. Knowledge of Go's DAG package model. Suggesting compiler workarounds over fixing design.
How do you widget-test states without real network calls?
This tests UI isolation from I/O. Mock the repository with Mockito, stub methods to yield loading, success, and error, and inject it via constructor or provider. Avoid real HTTP calls or unmocked clients in widget tests.
Replace a production NetworkService with a fake in Hilt instrumented tests
Tests whether you know Hilt test overrides beyond basic DI. A strong answer covers @BindValue for field-level fakes and @TestInstallIn or @UninstallModules for module swaps.
How would you access a Hilt dependency in a non-injectable ContentProvider?
Tests Hilt's escape hatch for framework classes. Strong answer: define an EntryPoint in SingletonComponent, expose the dependency, and retrieve it with EntryPointAccessors.fromApplication. Red flag: field injection or manual static singletons.
How do you inject two different OkHttpClient instances with Hilt?
Tests Hilt qualifiers for same-type bindings. Answer: define a custom @Qualifier (or @Named), annotate two @Provides methods and the injection site. Red flag: manual client creation or subclassing OkHttpClient instead of qualifying the binding.
Explain Hilt scoping: @Singleton vs @ActivityRetainedScoped
Tests Hilt component lifecycles. Answer: @Singleton lives for the app process; @ActivityRetainedScoped survives config changes via ActivityRetainedComponent but dies when the activity finishes.
How do you inject UserRepository into a ViewModel with Hilt?
Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.
What is Dependency Injection and Hilt's benefit over manual instantiation?
Tests your grasp of inversion of control and why frameworks matter. A good answer defines DI as supplying dependencies from outside, states Hilt automates object graph creation and scoping, and notes manual instantiation scatters construction logic.
How do you navigate between feature modules without direct dependencies?
This tests architectural decoupling in multi-module Android apps. A great answer describes a shared navigation contract or deep-link router that lets features stay independent. Recommending direct Gradle dependencies between features is a major red flag.
Accessing the Raw Request Object in FastAPI
Think of it as dropping to a lower level. Instead of FastAPI handing you validated data, you grab the raw Starlette HTTP request yourself. Use this for data not covered by standard declarations, like a client's IP.
FastAPI's Security Utility: Dependencies for Auth
FastAPI's `Security` utility is a specialized `Depends` for authentication. It signals to OpenAPI that a dependency is required for security, enabling interactive docs. Use it to protect endpoints by injecting the authenticated user.
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.