0Pricing
Kotlin Multiplatform Academy · Lekcja

Definiowanie interfejsu repozytorium

Udostępnianie potrzeb związanych z danymi bez ujawniania szczegółów HTTP

Definiowanie interfejsu repozytorium to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 1 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.

What a Repository Is

A repository is the single doorway your apps walk through to get data, hiding where that data actually comes from.

Start With an Interface

Define the repository as a Kotlin interface first, so callers depend on a contract instead of a concrete class.

interface UserRepository {
    suspend fun getUser(id: String): User
}

Speak in Domain Terms

Name methods after what the app needs, like getUser, not after how you fetch it. The intent stays clear and stable.

Return Domain Models

The interface returns clean domain models, never raw JSON or HTTP responses, so the UI stays simple.

suspend fun getUsers(): List<User>

No HTTP Words Allowed

Keep words like Ktor, headers, and status codes out of the interface. Callers should never know HTTP exists.

Suspend for Async Work

Mark data methods as suspend so they can do async work without blocking, and fit naturally into coroutines on both apps.

suspend fun refresh()

It Lives in commonMain

Put the interface in commonMain so Android and iOS share the exact same data contract with zero duplication.

Many Implementations Possible

One interface can have several implementations: a real network one and a fake one for tests, swapped freely.

Easier to Test

Because callers depend on the interface, you can inject a fake repository in tests and avoid hitting the real network.

class FakeUserRepository : UserRepository

Clear, Focused Methods

Keep each method small and purposeful. A focused API is easier to read, mock, and evolve over time.

A Complete Example

Here is a tidy repository contract that exposes everything the user feature needs and nothing more.

interface UserRepository {
    suspend fun getUsers(): List<User>
    suspend fun getUser(id: String): User
}

Quick Check

Where should the repository interface live in a KMP project?

Recap

A repository interface in commonMain defines what data the app needs using suspend methods and domain models, hiding all HTTP details.

Często zadawane pytania

Czy lekcja „Definiowanie interfejsu repozytorium” jest bezpłatna?

Tak — pełny tekst „Definiowanie interfejsu repozytorium” 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 „Definiowanie interfejsu repozytorium”?

Udostępnianie potrzeb związanych z danymi bez ujawniania szczegółów HTTP Ć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 1 z 4.

Ile czasu zajmuje lekcja „Definiowanie interfejsu repozytorium”?

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

  1. Definiowanie interfejsu repozytorium
  2. Ukrywanie API za repozytorium
  3. Mapowanie DTO na modele domenowe
  4. Udostępnianie wyników jako Flow
← Powrót do Kotlin Multiplatform Academy