Scoped DI¶
Once you represent your navigation in a Composable way, you will get powerful DI scoping as a pleasant side-effect:
Appyx gives every single Node
its own DI scope for free, with no extra effort required to clean up these scopes other than navigating away from them.
How does this work?¶
- Imagine you create an object in the
Node
related toOnboarding
, and make it available to all of its children. - While navigation is advancing between the individual screens of Onboarding,
O1
–O2
–O3
, this object will be the same instance. - As soon as the navigation switches to
Main
, the entire subtree ofOnboarding
is destroyed and all held objects are released. - Should the navigation ever go back to
Onboarding
, said object would be created from scratch.
This of course applies to every other Node
in the tree.
Scoping in practice¶
Imagine in a larger tree:
- A logout action is represented as switching the navigation back to the
Logged out
node - This will destroy the entire
Logged in
scope automatically - All objects held in the scope of an authenticated state are released without any special effort.
You could also use this for DI scoping flows (e.g. a cart object during a multi-screen product checkout).
With a regular approach these cases could be more difficult to represent:
- Screen-bound scopes wouldn't allow multi-screen lifetime of the objects.
- Application-scoped singletons would require extra attention of cleaning up once the flow ends.
With Appyx you get best of both worlds for free.