Building Scenic Projects with Layers
Explore how to design scenic Elixir projects by structuring external APIs, leveraging persistence functions, and managing state updates within OTP layers. Understand how to maintain modular code while allowing flexible persistence strategies and integrating Scenic with GenServer for interactive interfaces.
For this feature, let’s go from the outside in, addressing the outer API layer first. That way, we can think about the shape of our external APIs.
Pulling a persistence function from the environment
In /lib/mastery.ex, we need to pull a persistence function from the environment to dictate the persistence mechanism, like this:
Easy enough. We pass in another argument, a function to optionally persist each new response. The responsibility of the function from Mastery’s perspective is to return a response. We can use it any way we want. We might decide to save a response to a database or file or do nothing at all.