Przypadki użycia i granice domeny
Utrzymuj reguły biznesowe niezależne i łatwe do testowania.
Przypadki użycia i granice domeny to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎉
Często zadawane pytania
Czy lekcja „Przypadki użycia i granice domeny” jest bezpłatna?
Tak — pełny tekst „Przypadki użycia i granice domeny” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Przypadki użycia i granice domeny”?
Utrzymuj reguły biznesowe niezależne i łatwe do testowania. Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?
Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Przypadki użycia i granice domeny”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?
Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Warstwowa architektura dla KMP
- Przypadki użycia i granice domeny
- Podział współdzielonego kodu na moduły
- Zdecyduj, co pozostaje natywne