ユースケースとドメイン境界
ビジネスルールを独立させ、テスト可能に保ちます
「ユースケースとドメイン境界」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Rules Deserve a Home
Business rules scattered across screens drift and duplicate. Giving each rule its own use case creates one trusted place for important logic to live.
What a Use Case Is
A use case captures one app action, like place an order or refresh the feed. It reads as a verb and does exactly that one meaningful thing.
One Public Operator
A tidy use case exposes a single entry point. Many teams use Kotlin invoke so the object is called like a function at the call site.
class PlaceOrder(private val repo: OrderRepo) {
suspend operator fun invoke(cart: Cart): Order
}It Depends on Interfaces
A use case receives repository interfaces, never concrete clients. It orchestrates them but stays unaware of Ktor, SQL or any platform detail.
Pure and Testable
Because it has no UI and no I/O of its own, a use case is pure logic you can test in commonTest with simple fakes and zero device setup.
Drawing the Boundary
The domain boundary is the line use cases guard. Nothing technical, no JSON or HTTP types, crosses it; only clean models and clear results pass through.
Keep Frameworks Out
If a use case imports a Ktor or SQLDelight type, the boundary has leaked. Keep those framework imports in the data layer where they belong.
Composing Use Cases
Bigger flows combine smaller ones. A checkout use case might call validate cart, then place order, each independently tested and reused elsewhere.
Returning Outcomes
A use case usually returns a domain model or a sealed Result, so callers handle success and failure without catching framework exceptions.
suspend operator fun invoke(): Result<Feed>Who Calls Them
Shared ViewModels call use cases in response to user intents. The ViewModel coordinates; the use case decides. Each keeps a single, clear duty.
When to Skip Them
For a trivial passthrough, a use case can feel like ceremony. Add one when logic is real or shared; do not wrap a one-line call just for symmetry. 🙂
Quick Check
What belongs inside a well-designed use case?
Recap
You learned use cases hold one action each, depend on interfaces, and guard the domain boundary so frameworks and UI stay firmly outside. 🎉
よくある質問
「ユースケースとドメイン境界」レッスンは無料ですか?
はい。「ユースケースとドメイン境界」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「ユースケースとドメイン境界」で何を学びますか?
ビジネスルールを独立させ、テスト可能に保ちます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「ユースケースとドメイン境界」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- KMPのレイヤードアーキテクチャ
- ユースケースとドメイン境界
- 共有コードをモジュールに分割する
- ネイティブに残すものを決める