A recipe for the Perfect Use Case

A recipe for the Perfect Use Case

●9 ●22
calendar_today • schedule1 min read
— Originally published at medium.com

Today we’re cooking the perfect Use Case — and CQRS will be our recipe.

Ingredients

  • Action — fire and forget (logout, clear cache)
  • Query — read data (list products)
  • Command — write data (update profile)
  • Exchange — transform data (login)

All four types extend a sealed interface:

sealed interface UseCase<Input, Output> {
    interface Action : UseCase<Unit, Unit>
    interface Query<Output> : UseCase<Unit, Output>
    interface Command<Input> : UseCase<Input, Unit>
    interface Exchange<Input, Output> : UseCase<Input, Output>
}

Usecases

Three Ways to Season It

Arrow (Typed Errors) — the chef’s choice:

class GenerateSeed(
    private val seedService: SeedService
) : UseCase.Action {
    override suspend fun Raise<Throwable>.action() =
        seedService.generateSeed().bind()
}

Result (Standard Wrapper) — zero dependencies:

class GenerateSeed(private val service: SeedService) : UseCase.Action {
    override suspend fun action() = service.generateSeed().getOrThrow()
}

Raw (Direct Execution) — zero overhead:

class GenerateSeed(private val service: SeedService) : UseCase.Action {
    override suspend fun action() = service.generateSeed()
}

Why This Recipe Works

  • No more UseCase<Unit, Unit> noise
  • Every use case follows the same structure
  • Query or Command makes intent obvious

Full implementation in the GitHub repository.

Want more #architecture & #kotlin deep dives?

Join 6,048+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

Make for Small Business: What It Is, What It Costs, and When to Use It

floxolab - Oct 23

Short links that outlive the vendor: a newsroom URL shortener on WordPress and AWS

jaafarabazid - Nov 10

Free SEO Tools Every Small Business Should Use (2026) | FloxoLab

floxolab - Oct 20

The Audit Trail of Things: Using Hashgraph as a Digital Caliper for Provenance

Ken W. Algerverified - Apr 28
chevron_left
365 Points • 31 Badges
9Posts
4Comments
5Connections

Related Jobs

View all jobs →

Commenters (This Week)

14 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!