The Get.lazyReplace Method
Learn to replace dependencies lazily using Get.lazyReplace.
Replacing dependencies lazily
As the name suggests, Get.lazyReplace
replaces the dependencies lazily, i.e., only when Get.find
is called for the first time. Implementation-wise, it calls Get.lazyPut
internally, unlike Get.replace
, which calls Get.put
internally. This means that the original dependency is deleted from the memory immediately, but the new dependency is injected only when needed.
Features of Get.lazyReplace
Get.lazyReplace
comes bundled with three features that offer flexibility in replacing dependencies according to specific needs. The following are the features:
Conditionally replacing dependencies
Replacing specific instances
Reloading dependencies
Let us discuss each of them one by one.
Conditionally replacing dependencies
There are times when we want to initialize dependencies based on a condition. For example, we might choose between login and sign-up controllers based on whether the user is on the login or the sign-up page.
Get.lazyReplace
follows the builder pattern which lets us initialize the dependency conditionally. Instead of inputting the dependency directly, we provide it with a function that returns the dependency after performing logical operations. Here’s how it looks:
Get hands-on with 1400+ tech skills courses.