حقن التبعيات في ViewModels
احقن الخدمات للحفاظ على قابلية اختبار ViewModels
حقن التبعيات في ViewModels درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «حقن التبعيات في ViewModels» مجاني؟
نعم — نص درس «حقن التبعيات في ViewModels» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «حقن التبعيات في ViewModels»؟
احقن الخدمات للحفاظ على قابلية اختبار ViewModels تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «حقن التبعيات في ViewModels»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا يناسب MVVM لغة SwiftUI
- تصميم ViewModel
- ربط الواجهات بـ ViewModels
- حقن التبعيات في ViewModels