Ecto is not your application: how to register your persistence application?

Protocols revolve around a set of functions that can be applied to a data structure (i.e. a type) and as such are similar to interfaces in OO. That works if you want to hand an “instance” around that represents the particular database instance (pool or connection) and is absolutely necessary if you need to access a variety of distinct instances within the same runtime period.

But for the most part applications only actually deal with one single instance but need to retain the flexibility to use different, configurable implementations for different target environments (usually production, testing, development). In my opinion for that type of “swap-ability” a behaviour is a better fit.

Typically I’m not fond of Singletons (there rarely is only one Singleton in an application that uses Singletons) and in OO I will always favor Just Create One. But it seems that a Module/Behaviour is just about a perfect Singleton when you actually need one.

Mock Modules and Where to Find Them

1 Like