Skip to content

Dagger ‐ Scoping, normal example using singleton

Devrath edited this page Oct 6, 2023 · 11 revisions

Observations

  • Scoping is a concept that helps to limit the instance lifecycle to a certain timeline.
  • After the timeline is complete the instance is not active and if needed it has to be re-instantiated.
  • The @component can be considered as a container and all the annotated with the container will be singleton to this container.
  • In this example you can see that we have created an annotation which is basically a singleton and we added it into the @module and @Component areas.
  • This ensures that until the component reference is destroyed, No matter how many times we use the instance of a class, it returns the same memory reference.
  • But when we retate the screen, The activity is destroyed and recreated so the component is recreated in example below so observe there is a new memory address.

Output

// ----> When the Screen is first loaded connInterface1 reference---> 226246109 connInterface1 reference---> 226246109 // ----> App Screen is rotated and connInterface1 reference---> 8298091 connInterface1 reference---> 8298091 

Code

implementations

Connection.kt

interface Connection { fun connect(endpoint:String) }

HttpConnection.kt

class HttpConnection @Inject constructor() : Connection { override fun connect(endpoint: String) { PrintUtils.printLog("HttpConnection made") } }

HttpsConnection.kt

class HttpsConnection @Inject constructor(): Connection { override fun connect(endpoint: String) { PrintUtils.printLog("HttpsConnection made") } }

NetworkLibrary.kt

class NetworkLibrary @Inject constructor( val connection: Connection) { fun initilizeNetworkLibrary() { connection.connect("www.client.com") PrintUtils.printLog("Network library is initialized") } }

Scopes

ApplicationScope.kt

@Scope @MustBeDocumented @Retention(AnnotationRetention.RUNTIME) annotation class ApplicationScope

Modules

HttpConnectionModule.kt

@Module @DisableInstallInCheck class HttpConnectionModule { @Provides fun provideHttpConnection() : Connection { return HttpConnection() } }

HttpConnectionModule.kt

@Module @DisableInstallInCheck class HttpsConnectionModule { @ApplicationScope @Provides fun provideHttpsConnection() : Connection { return HttpsConnection() } }

Components

ConnectionComponent.kt

@ApplicationScope @Component(modules = [HttpsConnectionModule::class]) interface ConnectionComponent { fun inject(activity: IntroScopeDemoActivity) fun provideHttpsConnection() : HttpsConnection }

Activity

MyActivity.kt

class MyActivity : AppCompatActivity() { @Inject lateinit var connInterface1 : Connection @Inject lateinit var connInterface2 : Connection override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_intro_scope_demo) val comp = DaggerConnectionComponent.builder().build() comp.inject(this@IntroScopeDemoActivity) PrintUtils.printLog("connInterface1 reference---> "+connInterface1.hashCode().toString()) PrintUtils.printLog("connInterface1 reference---> "+connInterface2.hashCode().toString()) } }
Clone this wiki locally