Feature to Feature communication¶
Don't¶
At the initial launch of this library, the suggested way to make two Features
communicate with each other was to inject one in the constructor of the other.
This was before we elaborated on the Binder
, and it's not just no longer suggested, but it's straight out considered an anti-pattern.
Injecting them has many disadvantages:
- They become coupled
- Scoping becomes a potential problem (it works only as long the injected one is always supposed to outlive the other one)
- If you need two-way connection between them, it manifests itself as a circular dependency
Do¶
Use Binder
bindings to connect Features
as described in the relevant chapter:
binder.bind(feature1.news to feature2 using NewsToWish)
This is considered superior to the above injection-based approach in all aspects:
Features
can stay completely decoupled from one another- Scoping is automatically handled by
Binder
Binder
can even resubscribe a connection automatically if needed- In the case of a two-way connection, you can do it without a circular dependency, by adding a second binding.