เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน
ดูปัญหาที่ Hilt ช่วยแก้
เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 handEnter 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: โค้ดที่ทดสอบได้และแยกส่วน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน”
ดูปัญหาที่ Hilt ช่วยแก้ คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน
- โมดูล @Provides และ @Binds
- hiltViewModel และ @HiltViewModel
- ขอบเขตและอายุการใช้งานขององค์ประกอบ