SwiftUI Academy · บทเรียน

การฉีดการพึ่งพาสำหรับ ViewModels

ฉีดบริการเพื่อให้ทดสอบโมเดลมุมมองได้

บทเรียน 4 จาก 413 ขั้นตอน

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

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

The Hidden Dependency Problem

If a ViewModel creates its own network client inside itself, tests are stuck hitting the real server. That tight coupling makes it hard to swap later.

Inject Instead of Create

Dependency injection means passing in what a ViewModel needs from outside, rather than letting it build those services itself.

Define a Service Protocol

Describe the work with a protocol, not a concrete type. The ViewModel depends on the abstraction and never on a specific implementation.

protocol TaskService {
    func fetch() async -> [Task]
}

Accept It in the Initializer

Take the dependency through the init. Now any object that conforms to the protocol can be handed to the ViewModel.

init(service: TaskService) {
    self.service = service
}

Use the Injected Service

Inside methods, call the stored service rather than a hardcoded client. The ViewModel no longer cares who actually does the work.

tasks = await service.fetch()

A Real Implementation

Your production type conforms to the protocol and talks to the network. You inject it when building the real screen.

struct LiveTaskService: TaskService {
    func fetch() async -> [Task] { [] }
}

A Mock for Tests

For tests, write a mock that returns fixed data instantly. Inject it to verify ViewModel logic without any real network calls.

struct MockTaskService: TaskService {
    func fetch() async -> [Task] { sample }
}

Tests Become Trivial

With a mock injected, a test just calls a method and checks the result. No waiting, no flakiness, only fast and predictable runs. ✅

A Default for Convenience

Give init a default real service so everyday code stays short, while tests can still override it with a mock when needed.

init(service: TaskService = LiveTaskService())

Injecting via the Environment

For app-wide services, pass them through SwiftUI @Environment instead of every initializer, keeping call sites clean.

The Payoff

Injection makes ViewModels flexible and isolated. You can swap real, fake, or offline services without touching the view at all.

Quick Check

Why depend on a protocol instead of a concrete service?

Recap: Dependency Injection

You learned to inject services through a protocol, swap real for mock in tests, and even use @Environment for shared dependencies. That completes MVVM.

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

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

บทเรียน “การฉีดการพึ่งพาสำหรับ ViewModels” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การฉีดการพึ่งพาสำหรับ ViewModels”

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

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

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

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

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

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

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

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

  1. เหตุใด MVVM จึงเหมาะกับ SwiftUI
  2. การออกแบบ ViewModel
  3. เชื่อมมุมมองกับ ViewModels
  4. การฉีดการพึ่งพาสำหรับ ViewModels
← กลับไปที่ SwiftUI Academy