In a mobile app's dependency-injection design, how do app, session and screen scopes map onto modules, and what must happen to session-scoped objects at logout or account switch?
answer
- one composition root, in the app module
- the object's lifetime decides its scope
- anything that knows the user is session
- logout destroys the session graph whole
- wider scopes never hold narrower ones
basics
~20 sThe app module, as composition root, builds an app scope for process-wide objects, a session scope opened at sign-in for anything tied to one user, and screen scopes. Logout or account switch destroys the session scope whole.
solid answer
~50 sA **scope** is a lifetime: objects in it are created once and shared until the scope ends. The **composition root**, the one place that builds the object graph, lives in the app module because only it sees every implementation. **App scope** holds what is valid for the whole process and any user: the base HTTP client without credentials, image decoding, logging. **Session scope** opens at sign-in and holds everything that knows who the user is: the user's repositories and in-memory caches, the authenticated API wrappers. **Screen scope** lasts as long as one screen or flow. At logout or account switch, the root closes the session scope as a unit: cancel its work, drop its caches, then build a fresh one for the next user. The classic bug is a user-specific object registered at app scope, which survives logout and shows one user's data to the next.
go deeper
Recall the three lifetimes: the process, the signed-in user, one screen. Anything that knows who the user is lives in the session scope.
Explain why the composition root sits in the app module, and why dependencies may point only from narrower scopes to wider ones.
Design logout and account switch as closing one scope: cancel its work, drop its caches, discard late results, rebuild from nothing. Diagnose the user-data leak to a mis-scoped object.
Make scope placement a reviewed contract: features declare their scope, the platform team owns the root and the session lifecycle, and a test signs in, out and in again to prove nothing leaks.
## Scopes are lifetimes **Dependency injection** means an object receives what it needs from outside instead of constructing it. An **injection container** (hand-written or generated) builds those objects, and a **scope** tells it how long each one lives: created once and shared within the scope, discarded when the scope ends. Choosing a scope is choosing a lifetime, and in a mobile app the lifetimes that matter are the process, the signed-in user, and a screen. ## The composition root lives in the app module The **composition root** is the single place where the object graph is assembled and each interface is bound to an implementation. In a modularized app it belongs in the **app module**, because that is the only module that depends on every feature's implementation. Feature modules do not construct their collaborators and do not look them up from a global; they **declare** what they need, usually as constructor parameters typed by API-module interfaces, and the root supplies it. That keeps the module graph honest: a feature cannot quietly reach another feature's implementation through the container. ## Three scopes and what goes in each | Scope | Opens | Closes | Holds (super-app examples) | May depend on | |---|---|---|---|---| | **App** | Process start | Process end | Base HTTP client with no credentials, image decoding, logging, the local clock | App scope only | | **Session** | Sign-in, or restore of a signed-in user | Logout, account switch, forced sign-out | The user's ride history repository, saved payment methods cache, wallet balance stream, the authenticated API wrapper | Session and app | | **Screen** | A screen or flow opens | It is gone for good | One screen's state holder, a checkout flow's in-progress selections | Screen, session and app | The test for each object is simple: **does it know who the user is?** If it holds, caches or can fetch anything belonging to one account, it is session-scoped, however convenient a process-wide singleton would be. ## Scopes as module boundaries - **Dependencies point from narrower lifetimes to wider ones**: screen to session to app. A screen-scoped view model may use a session-scoped repository; never the reverse. - **A wider scope must never hold a narrower object.** An app-scoped analytics client that keeps a reference to the session's user repository keeps that repository alive after logout, a so-called captive dependency. - **Each feature contributes bindings to the scope it belongs in**, through its implementation module, and the root assembles them; the feature does not decide when a session starts or ends. - **The session scope is an object the root can close**, not a set of flags scattered across modules. ## Logout and account switch When the user logs out, or switches from a personal to a business account, the root: 1. **Stops new work** for the old session: screens leave the signed-in flow so nothing new is requested. 2. **Cancels in-flight work** owned by the session scope: network calls, observers, timers. Work the OS runs later in the background must be cancelled or must check which session scheduled it. 3. **Closes the session scope** as a unit, dropping every in-memory cache and stream it held. 4. **Leaves the user's on-disk data to the storage policy**, which decides what is wiped at logout; that is a storage and device-security decision, not a container one. 5. **Opens a fresh session scope** at the next sign-in, built from nothing. A late response from a cancelled request must be **dropped**, because its session no longer exists; tagging work with the session that started it makes that check easy. ## The bugs this prevents - User B briefly sees user A's rides because the ride repository was registered at app scope. - A wallet balance stream keeps polling with A's credentials after logout. - An account switch resets most objects by hand but one forgotten cache leaks the old account. - Memory grows with each sign-in because old session objects are held by app-scoped ones. Tearing the session scope down and building a new one makes correctness **structural**: no object has to remember to reset itself.
- Why is it a bug for an app-scoped object to depend on a session-scoped one?The app-scoped object outlives the session, so it keeps the session object alive after logout, a captive dependency. It can then serve the previous user's cache or credentials to the next user, and memory grows with each sign-in. Dependencies may point only from narrower lifetimes to wider ones: screen to session to app.
- An account switch is faster without a teardown. Can you keep the session scope and just swap the user id?Not safely. Every session object would have to reset itself correctly, and one that forgets, a cache, a pending request, a stream, leaks the old account into the new one. Rebuilding the session scope makes correctness structural; only user-independent objects in app scope carry over the switch.
- What happens to a request still in flight when the user logs out?It belongs to the session scope, so closing the scope cancels it. If a response still arrives, it must be discarded because its session no longer exists. Tagging work with the session that started it lets late results be recognised and dropped instead of written into the next user's state.
A hotel keeps the building, the lifts and the lobby for every guest (app scope), but each stay gets a room key, a minibar tab and a wake-up call list (session scope). At checkout the whole stay is closed out together; nobody hands the next guest a room with the last guest's tab still running.
- Put everything in app scope; singletons are simpler and nothing breaks at logout.
- Logging out only needs to delete the stored access token.
- An app-scoped object can safely keep a reference to a session-scoped one.
- Each feature module should build its own HTTP client and repositories itself.
- Scopes are a performance tuning knob and have nothing to do with correctness.