ห่อ API ไว้เบื้องหลัง Repository
ซ่อน Ktor และ serialization จากผู้เรียกใช้
ห่อ API ไว้เบื้องหลัง Repository เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “ห่อ API ไว้เบื้องหลัง Repository” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ห่อ API ไว้เบื้องหลัง Repository” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ห่อ API ไว้เบื้องหลัง Repository”
ซ่อน Ktor และ serialization จากผู้เรียกใช้ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ห่อ API ไว้เบื้องหลัง Repository” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กำหนดอินเทอร์เฟซ Repository
- ห่อ API ไว้เบื้องหลัง Repository
- แมป DTO เป็นโมเดลโดเมน
- เปิดเผยผลลัพธ์เป็น Flow