0Pricing
Jetpack Compose Academy · درس

لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة

شاهد المشكلات التي يحلها Hilt

لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What Is Dependency Injection?

Dependency injection means a class receives the objects it needs from the outside, instead of building them itself. Less wiring, more focus. 🔌

The Problem: new Everywhere

When a class calls new on its own dependencies, it gets locked to those exact types. Changing one thing forces you to edit many files.

class Repo {
    private val api = ApiService() // hard-wired
}

Inject Instead of Construct

Pass dependencies through the constructor and let something else create them. Now your class only states what it needs, not how to build it.

class Repo(private val api: ApiService) {
    // api is given to us
}

Decoupling Made Real

With injection your code depends on an interface, not a concrete class. You can swap the real one for another without touching the consumer.

interface ApiService { fun load(): String }
class Repo(private val api: ApiService)

Testability Is the Big Win

In tests you simply pass a fake dependency. No network, no database, no surprises, just fast and predictable checks. ✅

val fake = object : ApiService { override fun load() = "hi" }
val repo = Repo(fake)

Manual DI Gets Tedious

You can inject by hand, but wiring deep graphs means writing lots of boilerplate. Every new dependency ripples up through the constructors.

val api = ApiService()
val repo = Repo(api)
val vm = MyViewModel(repo) // by hand

Enter Hilt

Hilt is Android's recommended DI library. It generates the wiring for you at compile time, so you describe needs and Hilt builds the graph.

Hilt Sits on Top of Dagger

Hilt is built on Dagger but adds Android-aware defaults. You get compile-time safety with far less setup than raw Dagger.

Mark Your Application

Hilt starts at the app root. Annotate your Application class with @HiltAndroidApp to generate the top-level dependency container.

@HiltAndroidApp
class MyApp : Application()

Inject With @Inject

Add @Inject to a constructor and Hilt learns how to create that type. From then on it can supply it anywhere it is needed.

class Repo @Inject constructor(private val api: ApiService)

Compile-Time Safety

If a dependency is missing, Hilt fails at compile time, not at runtime. You catch wiring mistakes before the app ever ships. 🛡️

Quick Check

Which benefit best captures why we use dependency injection?

Recap: Why DI Matters

You learned that DI hands a class its dependencies, decoupling code and making tests trivial. Hilt automates that wiring at compile time. 🎉

الأسئلة الشائعة

هل درس «لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة» مجاني؟

نعم — نص درس «لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

ماذا ستتعلم في «لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة»؟

شاهد المشكلات التي يحلها Hilt تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟

لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟

نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة
  2. الوحدات و@Provides و@Binds
  3. ‏hiltViewModel و@HiltViewModel
  4. النطاقات وأعمار المكونات
← العودة إلى Jetpack Compose Academy