Clean Architecture & Use Cases Quiz

ARCHITECTURE › Patterns

According to the dependency rule, which dependency direction is allowed?

Answer: The UI layer depends on the domain layer, which depends on data

Dependencies point inward: UI depends on domain, domain depends on data. Inner layers (domain, data) never depend on the outer UI layer.

Which naming follows Android's convention for a use case?

Answer: GetLatestNewsWithAuthorsUseCase: verb + noun + UseCase

The convention is verb in present tense + noun + 'UseCase'. GetLatestNewsWithAuthorsUseCase matches; the others are a manager, a repository, and a generic helper.

Why should the domain layer avoid Android framework dependencies?

Answer: So the business logic stays pure Kotlin, reusable and testable

Keeping the domain layer pure Kotlin makes it reusable (Wear, TV, KMP) and testable on the JVM with plain unit tests, independent of the volatile Android SDK.

When is introducing a use case LEAST justified?

Answer: When it is a thin one-to-one pass-through to one repository method

Android's guidance warns that wrapping a simple single-repository call in a use case adds complexity for little benefit. Reuse, combining repositories, and complex rules are the cases that justify one.

What does it mean that domain-layer use cases must be 'main-safe'?

Answer: Safe to call on the main thread; it moves blocking work off it.

Main-safe means a caller can invoke the use case on the main thread; the use case itself shifts any blocking work to a background dispatcher, e.g. via withContext.

How are use cases commonly made callable like a function in Kotlin?

Answer: By defining operator fun invoke()

Defining operator fun invoke() lets you call an instance like a function, e.g. getLatestNews(). No base class or annotation is required.

How does the official guidance classify the domain layer?

Answer: Optional; recommended in big apps to reuse logic or ease ViewModel load

The UI and data layers are strongly recommended, while the domain layer is optional and recommended in big apps when you need to reuse data-layer logic or simplify a complex ViewModel.

How does Clean Architecture apply dependency inversion to a repository the domain needs?

Answer: The repository interface lives in the domain, and data supplies the impl.

Dependency inversion puts the repository abstraction (interface) in the domain and the concrete implementation in the data layer, so the inner domain depends on an interface it owns rather than on outer framework details.

Can a use case depend on another use case?

Answer: Yes, a use case can depend on other use cases and on repositories

Android's guidance explicitly allows use cases to depend on other use cases and on repositories, letting you compose higher-level operations from smaller ones.

Why do many apps define separate domain models instead of passing network DTOs and database entities straight through?

Answer: So each layer can evolve independently without coupling domain to API or DB shapes

Distinct per-layer models with mapping between them keep the domain from being coupled to volatile network or persistence formats, so a schema change in one layer doesn't ripple through the others.

Why are use cases designed to be stateless and created fresh each time they are injected?

Answer: They own no lifecycle, and mutable state would make sharing one unsafe

Use cases have no lifecycle and should hold no mutable data; creating a new instance per injection keeps them safe to reuse across callers without shared-state bugs.

Besides use cases, what else legitimately belongs in the domain layer?

Answer: Domain models (entities) and repository interfaces

The domain layer holds business entities and the repository abstractions use cases depend on; Activities, DAOs, Retrofit services, and Composables all belong to the outer UI or data layers.

Which of the following would violate the design of a domain-layer use case?

Answer: Holding a reference to an Android Context or View

Referencing Android framework types like Context or View couples the domain to the SDK, breaking its purity; the other three are exactly how a well-formed use case is meant to behave.

In the recommended architecture, which component typically depends on use cases and exposes state to the UI?

Answer: The ViewModel in the UI layer

ViewModels sit in the UI layer, call use cases (or repositories) to run business logic, and expose the resulting observable state; repositories and data sources sit below and Composables consume the ViewModel's state rather than invoking use cases themselves.

Back to Clean Architecture & Use Cases