Repositoryの背後にAPIをラップする
呼び出し側からKtorとSerializationを隠します
「Repositoryの背後にAPIをラップする」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Now Build the Real Thing
With the interface set, you write a concrete implementation that actually fetches data behind the scenes.
Implement the Interface
Create a class that uses implements the repository interface, promising to fulfill every method it declares.
class UserRepositoryImpl : UserRepositoryInject the Ktor Client
Pass the HttpClient into the constructor instead of creating it inside, so it can be shared and replaced in tests.
class UserRepositoryImpl(private val client: HttpClient) : UserRepositoryHide the Network Call
Inside each method, make the Ktor request and return the result. Callers see clean data, never the request itself.
override suspend fun getUsers() = client.get(url).body<List<User>>()Keep URLs Inside
Store base URLs and paths within the implementation. The rest of the app stays blissfully unaware of any endpoint.
One Place to Change
If the API path changes, you edit only this class. The interface and every caller stay untouched.
Serialization Stays Hidden
The body call turns JSON into models here, so serialization never leaks out to the UI layer.
val user: User = client.get(url).body()Same Code, Both Apps
This implementation lives in commonMain, so the same fetching code powers Android and iOS identically.
Swap Without Breaking Callers
Because it satisfies the interface, you can replace it with a caching version later and no caller changes.
Wire It With DI
A dependency injection setup hands callers this implementation through the interface type, keeping things loosely coupled.
single<UserRepository> { UserRepositoryImpl(get()) }The Full Wrapper
Here is the complete wrapper: it takes a client, hides the request, and returns plain domain models.
class UserRepositoryImpl(
private val client: HttpClient
) : UserRepository {
override suspend fun getUsers() =
client.get("/users").body<List<User>>()
}Quick Check
What is the main job of the repository implementation?
Recap
The implementation takes an HttpClient, performs the request, and returns domain models, keeping all networking sealed off from callers.
よくある質問
「Repositoryの背後にAPIをラップする」レッスンは無料ですか?
はい。「Repositoryの背後にAPIをラップする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「Repositoryの背後にAPIをラップする」で何を学びますか?
呼び出し側からKtorとSerializationを隠します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Repositoryの背後にAPIをラップする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Repository Interfaceを定義する
- Repositoryの背後にAPIをラップする
- DTOをDomain Modelsにマッピングする
- ResultsをFlowとして公開する