โมดูล @Provides และ @Binds
บอก Hilt วิธีสร้างชนิดของคุณ
โมดูล @Provides และ @Binds เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
When Constructor Injection Is Not Enough
You cannot add @Inject to a constructor you do not own, like a Retrofit or OkHttp instance. Hilt needs another way to learn how to build those.
Meet the Module
A module is a class that teaches Hilt how to create types it cannot construct on its own. Mark it with @Module to register it.
@Module
object NetworkModule { }Install Into a Component
Every module declares where it lives with @InstallIn. SingletonComponent makes its bindings available app-wide for the whole process.
@Module
@InstallIn(SingletonComponent::class)
object NetworkModuleProvide a Type With @Provides
@Provides marks a function whose return value Hilt will use as a dependency. The function body shows exactly how to build it.
@Provides
fun provideApi(): ApiService = Retrofit.Builder().build().create(ApiService::class.java)Provides Can Take Parameters
A @Provides function can ask for other dependencies as parameters. Hilt supplies them first, then runs your function to assemble the result.
@Provides
fun provideRepo(api: ApiService): Repo = Repo(api)Binding Interfaces With @Binds
When you just need to map an interface to an implementation, @Binds is leaner than @Provides. It tells Hilt which class satisfies the interface.
@Binds
abstract fun bindRepo(impl: RepoImpl): Repo@Binds Needs an Abstract Module
Because @Binds methods are abstract, their module must be an abstract class, not an object. The implementation must already be injectable.
@Module
@InstallIn(SingletonComponent::class)
abstract class RepoModuleProvides vs Binds
Use @Binds for a simple interface-to-class mapping, and @Provides when you must run code to build the object. Binds generates less code.
Mark Singletons
Add @Singleton to a provider so Hilt creates the object once and reuses it. Perfect for heavy clients like a shared OkHttp instance.
@Provides
@Singleton
fun provideClient(): OkHttpClient = OkHttpClient()Disambiguate With Qualifiers
If two providers return the same type, a @Qualifier annotation tells them apart. Hilt then knows which binding each injection point wants.
@Qualifier
annotation class AuthClientModules Build the Graph
Together your modules form the dependency graph. Hilt reads them at compile time and wires every type into a connected, verified tree.
Quick Check
You need to bind a Repo interface to its single RepoImpl class. Which is the leanest choice?
Recap: Teaching Hilt to Build
You saw that modules teach Hilt how to create types it cannot construct, using @Provides for code and @Binds for interface mappings. 🧩
คำถามที่พบบ่อย
บทเรียน “โมดูล @Provides และ @Binds” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โมดูล @Provides และ @Binds” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โมดูล @Provides และ @Binds”
บอก Hilt วิธีสร้างชนิดของคุณ คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “โมดูล @Provides และ @Binds” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน
- โมดูล @Provides และ @Binds
- hiltViewModel และ @HiltViewModel
- ขอบเขตและอายุการใช้งานขององค์ประกอบ