Skip to content
Back to skills

Android Domain Layer

ASecurity

Use cases per Google's domain layer guide: when to add one, naming, operator invoke, main-safety, lifetime, testing. Use when creating, reviewing, or deciding whether to add a use case, and when business logic is duplicated across ViewModels or one ViewModel combines too many repositories.

  • 5 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 18, 2026
developmentgokotlintesting

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 18, 2026

npx -y skills add pablitoarango/android-skills --skill android-domain-layer --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Android Domain Layer?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Android Domain Layer
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/pablitoarango-android-domain-layer/badge)](https://www.skillsdirectory.com/skills/pablitoarango-android-domain-layer)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: android-domain-layer
description: "Use cases per Google's domain layer guide: when to add one, naming, operator invoke, main-safety, lifetime, testing. Use when creating, reviewing, or deciding whether to add a use case, and when business logic is duplicated across ViewModels or one ViewModel combines too many repositories."
---

# Android Domain Layer

Sources of truth: [Domain layer](https://developer.android.com/topic/architecture/domain-layer), [Architecture recommendations](https://developer.android.com/topic/architecture/recommendations). Rules marked **(stricter than Google)** are deliberate choices.

## 1. Decide whether a use case is needed

The domain layer is **optional**; Google rates it recommended in big apps. Add a use case only when at least one is true:

- The same business logic is needed by **more than one ViewModel**.
- The logic makes a ViewModel **hard to read or test** (combining several repositories, multi-step rules).

Otherwise the ViewModel calls the repository directly. A use case that only forwards one repository call adds a class without adding value.

Complex computations that benefit from caching usually belong in the **data layer**, not in a use case. When Room is the source of truth and the use case would only join related entities, write a Room relationship query in a repository (`NewsWithAuthorsRepository`) instead.

## 2. Shape of a use case

- One use case, one responsibility.
- Name: **verb in present tense + optional noun + `UseCase`**: `FormatDateUseCase`, `LogOutUserUseCase`, `GetLatestNewsWithAuthorsUseCase`.
- Expose `operator fun invoke` so callers use it like a function; overloads may support different inputs to the same responsibility.
- Suspending one-shot work uses `suspend operator fun invoke(...)`; synchronous work can use a regular `invoke`, and observable results return `Flow`.
- **Main-safe**: if the use case does blocking or CPU-heavy work, it moves that work to an **injected** dispatcher.
- **No mutable state.** Keep mutable data in the UI or data layer.
- **(Stricter than Google)** Pure Kotlin: no `android.*` imports.

```kotlin
class GetLatestNewsWithAuthorsUseCase @Inject constructor(
    private val newsRepository: NewsRepository,
    private val authorsRepository: AuthorsRepository,
    @Dispatcher(Default) private val defaultDispatcher: CoroutineDispatcher,
) {
    suspend operator fun invoke(): List<ArticleWithAuthor> = withContext(defaultDispatcher) {
        val news = newsRepository.fetchLatestNews()
        news.map { article ->
            ArticleWithAuthor(article, authorsRepository.getAuthor(article.authorId))
        }
    }
}
```

Combining streams:

```kotlin
class GetFollowableTopicsUseCase @Inject constructor(
    private val topicsRepository: TopicsRepository,
    private val userDataRepository: UserDataRepository,
) {
    operator fun invoke(): Flow<List<FollowableTopic>> = combine(
        userDataRepository.getUserDataStream(),
        topicsRepository.getTopicsStream(),
    ) { userData, topics ->
        topics.map { FollowableTopic(it, isFollowed = it.id in userData.followedTopics) }
    }
}
```

Calling it:

```kotlin
@HiltViewModel
class ForYouViewModel @Inject constructor(
    getFollowableTopics: GetFollowableTopicsUseCase,
) : ViewModel() {
    val uiState: StateFlow<ForYouUiState> = getFollowableTopics()
        .map(ForYouUiState::Success)
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ForYouUiState.Loading)
}
```

## 3. Dependencies

- A use case may depend on **repositories** and on **other use cases**.
- The domain layer depends on the data layer, never the other way around.
- Nothing in the domain layer depends on the UI layer.

## 4. Lifetime and injection

- Use cases have no lifecycle of their own; they live as long as the class that uses them.
- Callers are not limited to ViewModels: services, the `Application` class, and the UI layer of other form factors (TV, Wear) sharing the codebase can reuse them.
- **Do not scope** use cases in Hilt (no `@Singleton`, `@ViewModelScoped`). A new instance per consumer is expected.

## 5. Restricting data layer access

Google describes two options:

- **Flexible (default)**: ViewModels may call repositories directly and use cases only where section 1 applies.
- **Strict**: all data access goes through use cases. Adopt it for a feature only when it already calls use cases almost exclusively, or when a cross-cutting concern (for example analytics on every access) needs one choke point.

## 6. Testing

- Unit test use cases on the JVM, preferring **fake repositories** (`FakeNewsRepository`); choose other test doubles when the behavior under test requires them.
- Inject a `TestDispatcher` for the dispatcher parameter and run tests with `runTest`.
- See `android-testing`.

## 7. Checklist

- [ ] The use case is shared by several ViewModels or removes real complexity.
- [ ] Named verb + optional noun + `UseCase`, exposes `operator fun invoke` for one responsibility.
- [ ] Main-safe; dispatchers are injected.
- [ ] Holds no mutable state and is not scoped.
- [ ] Depends only on repositories and other use cases; no `android.*` imports.

Files in this skill

  • SKILL.md5.1 KB
  • agents/openai.yaml125 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.