Dependency Injection

Understanding Dependency Injection in Android

Introduction to Dependency Injection

Dependency Injection (DI) एक design pattern है जो classes को उनके dependencies को externally provide करने की allow करता है। यह code को more maintainable और testable बनाता है।

Dependency Injection (DI) is a design pattern that allows classes to have their dependencies provided externally. It makes code more maintainable and testable.

Key Features (मुख्य विशेषताएं):

  • DI dependencies को externally manage करता है
  • DI manages dependencies externally
  • DI code को more testable बनाता है
  • DI makes code more testable
  • DI code को more maintainable बनाता है
  • DI makes code more maintainable
  • DI dependencies को easily replace करने की allow करता है
  • DI allows easy replacement of dependencies

Types of DI (डिपेंडेंसी इंजेक्शन के प्रकार)

Android में DI के main types:

Main types of DI in Android:

Type (प्रकार) Description (विवरण)
Constructor Injection Dependencies को constructor के through provide किया जाता है
Constructor Injection Dependencies are provided through constructor
Field Injection Dependencies को fields में directly inject किया जाता है
Field Injection Dependencies are injected directly into fields
Method Injection Dependencies को methods के through provide किया जाता है
Method Injection Dependencies are provided through methods

Implementation Example (इम्प्लीमेंटेशन उदाहरण)

Hilt (Dagger) का use करके DI को implement करने का example:

Example of implementing DI using Hilt (Dagger):

// Add Hilt dependencies
dependencies {
    implementation 'com.google.dagger:hilt-android:2.48'
    kapt 'com.google.dagger:hilt-compiler:2.48'
}

// Application class
@HiltAndroidApp
class MyApplication : Application()

// Define dependencies
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    @Singleton
    fun provideApiService(): ApiService {
        return Retrofit.Builder()
            .baseUrl("https://api.example.com/")
            .build()
            .create(ApiService::class.java)
    }
}

// Use dependencies in ViewModel
@HiltViewModel
class MyViewModel @Inject constructor(
    private val apiService: ApiService
) : ViewModel() {
    fun fetchData() {
        viewModelScope.launch {
            val data = apiService.getData()
            // Handle data
        }
    }
}

// Use in Activity
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    @Inject
    lateinit var viewModel: MyViewModel
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        viewModel.fetchData()
    }
}

Best Practices (सर्वोत्तम प्रथाएं)

DI के साथ काम करने के best practices:

Best practices for working with DI:

  • Constructor Injection: Constructor injection का prefer करें
  • Constructor Injection: Prefer constructor injection
  • Scoping: Appropriate scopes का use करें
  • Scoping: Use appropriate scopes
  • Testing: DI का use करके testing को easy बनाएं
  • Testing: Make testing easier with DI
  • Modularity: Code को modular बनाएं
  • Modularity: Keep code modular