XPEHO / XpeApp

BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Quelle méthode choisir pour l'injection de dépendances #88

Open lcubeddu opened 3 months ago

lcubeddu commented 3 months ago

Pour moi il y a 3 options

Le choix a ultimement assez peu de conséquences étant donné la taille du projet, donc plutôt basse priorité

Pros & Cons

Examples de la syntaxe

Dagger/Hilt

// Application.kt

@HiltAndroidApp  
class ExampleApplication : Application() { ... }

// .......

// Activity.kt

@AndroidEntryPoint  
class ExampleActivity : AppCompatActivity() { ... }

// ......

// constructor of a class with IOC

class AnalyticsAdapter @Inject constructor(
    private val service: AnalyticsService  
) { ... }

// module with interface injection

@Module
@InstallIn(ActivityComponent::class)
abstract class AnalyticsModule {

  @Binds
  abstract fun bindAnalyticsService(
    analyticsServiceImpl: AnalyticsServiceImpl
  ): AnalyticsService
}

// module with instance injection

@Module
@InstallIn(ActivityComponent::class)
object AnalyticsModule {

  @Provides
  fun provideAnalyticsService(
    // Potential dependencies of this type
  ): AnalyticsService {
      return Retrofit.Builder()
               .baseUrl("https://example.com")
               .build()
               .create(AnalyticsService::class.java)
  }
}

Koin

// Module

class MyService()
class Controller()

val myModule = module {
    // declare single instance for MyService class
    single { MyService() }

    // declare factory instance for Controller class
    factory { Controller() }
}

//......
// Dependency resolution

// Presenter <- Service
class Service()
class Controller(val view : View)

val myModule = module {

    // declare Service as single instance
    single { Service() }
    // declare Controller as single instance, resolving View instance with get()
    single { Controller(get()) }
}

Manuelle

Voir la pull request suivante : https://github.com/XPEHO/XpeApp/pull/84

Notamment, les fichiers XpeApp.kt et AppModule.kt

lcubeddu commented 3 months ago

@mmarchal