0Pricing
Kotlin Multiplatform Academy · บทเรียน

ฉีดการพึ่งพาเข้า ViewModels และรีโพ

แก้ไขการพึ่งพาอย่างเป็นระเบียบทั่วทั้งแอป

ฉีดการพึ่งพาเข้า ViewModels และรีโพ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

From Declared to Used

You registered dependencies and started Koin. Now comes the payoff: injecting those objects into your repositories and view models without building them by hand.

Constructor Injection First

The cleanest style is constructor injection: list what a class needs as parameters, and Koin fills them via get() in the module.

single { UserRepository(api = get()) }

A Repo That Takes Deps

Your repository simply declares its needs in the constructor. It never touches Koin, which keeps it easy to test in isolation.

class UserRepository(private val api: ApiClient)

Inject into a ViewModel

A shared ViewModel takes the repository through its constructor too, so all of its data comes from one injected source.

class UserViewModel(private val repo: UserRepository)

Register the ViewModel

Add the view model to your module. A factory fits well, giving each screen its own fresh instance.

factory { UserViewModel(get()) }

Resolve from Android Compose

On Android the koinViewModel() helper hands a Compose screen the wired-up view model in a single call.

val vm: UserViewModel = koinViewModel()

Resolve from iOS

From Swift you ask Koin for the same view model through a small bridge function, so SwiftUI gets an identically wired object. 🍏

Avoid the Service Locator Trap

Prefer constructor injection over calling get() deep inside classes. Hidden lookups make code harder to read, reuse, and test.

Inject Interfaces, Not Classes

Depend on an interface and let Koin supply the implementation. Swapping in a fake for tests then becomes a one-line module change.

single<UserRepository> { UserRepositoryImpl(get()) }

The Whole Chain

Notice the chain Koin assembles for you: client builds repo, repo builds use case, use case builds view model, all from constructors.

factory { UserViewModel(get(), get()) }

Testing Becomes Easy

Because everything arrives through constructors, your tests just pass fakes directly, no Koin required, keeping unit tests fast and predictable.

Quick Check

Choose the cleanest injection style.

Recap

You injected dependencies through constructors into repos and view models, resolved them per platform, and saw why interfaces plus constructor injection make testing a breeze. 🎯

คำถามที่พบบ่อย

บทเรียน “ฉีดการพึ่งพาเข้า ViewModels และรีโพ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ฉีดการพึ่งพาเข้า ViewModels และรีโพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ฉีดการพึ่งพาเข้า ViewModels และรีโพ”

แก้ไขการพึ่งพาอย่างเป็นระเบียบทั่วทั้งแอป คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ฉีดการพึ่งพาเข้า ViewModels และรีโพ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ประกาศโมดูล Koin แบบ shared
  2. ขอบเขต single เทียบกับ factory
  3. เริ่มต้น Koin บน Android และ iOS
  4. ฉีดการพึ่งพาเข้า ViewModels และรีโพ
← กลับไปที่ Kotlin Multiplatform Academy