Companions

Description

Companions are stateless modules that contain one or many methods related to the same area. Ideally, each companion method is a pure function, but it may be practical to read from the DB or Redis cache. Companions should avoid side-effects, writing to the DB or handling failure cases (under normal conditions). If your companion does these things, it should likely be refactored into an Actor. Often, private methods in Actors or controllers can be extracted to Companions for easier testing and/or re-use.

Technology Status at Wunder

Adopt. Companions are a good way to extract and test complex logic without the boilerplate imposed by Actors.

Notes/Resources

  • Companions should be stateless, however you might find it convenient to initialize a companion with data. If this is needed, consider turning the companion into a simple Model. Otherwise, be pragmatic, but remember that ideally companions are stateless.
  • If you have an Actor which a) has a name like ‘Compute*’ b) never nacks c) has no side effects, you should consider converting the entire actor (or a substantial portion of it) to a Companion.