Ash in an ALA app: which pieces fit, which don't, and where the rest goes
The LiveView post asked how close a Phoenix page can come to Spray’s application layer. This one asks the same about the other framework an Elixir team is likely to adopt. Ash is popular, declarative and well engineered, and its central idea, a resource that is the single source of truth from which everything else is derived, is the idea Spray spends §6.17 arguing against. So the question isn’t whether Ash is “good” but which of its pieces an ALA app can use, in which layer, and what to do with the ones it can’t. Spray says nothing about Ash; every placement below is the checklist’s reading, with the rule it rests on.
What kind of framework Ash is
Ash calls itself a declarative, resource-oriented application framework. You declare an Ash.Resource: its attributes, relationships to other resources, actions (create, read, update, destroy, and generic ones), the changes and validations that run inside an action, calculations and aggregates over its data, identities, policies for authorization, and a data layer (AshPostgres, ETS, Mnesia). Extensions then derive the rest from that declaration: AshPhoenix builds forms, AshJsonApi and AshGraphql build APIs, AshAuthentication adds users, AshStateMachine adds a state field with transitions, AshOban schedules work against records, and the notifiers publish what an action did. Resources are grouped by an Ash.Domain, whose code interface (define :add_line) is how the rest of the app calls them.
Three characteristics matter for ALA:
- The resource is the hub. Everything attaches to the entity: its behaviour (actions), its rules (changes, validations, policies), its derived data (calculations, aggregates), its relationships, its side effects (notifiers, triggers). That is the “class per business noun, holding data, accessors and the behaviour of every use case that touches it” the OO checklist names as the entity Spray rejects: entities “will tend to hold some fields that, although they associate with an identity, really belong to separate use cases” (§6.17.2).
- It is a DSL over Spark, so declarations are knowledge dependencies on Ash. A resource
usesAsh.Resource; a change implementsAsh.Resource.Change; a notifier implementsAsh.Notifier. Each is a module configuring a far more general framework, which R9 allows: the same as implementingGenServeror LiveView’s callbacks. The framework itself sits in Programming Paradigms and Foundation, library-provided. - It composes by containment, not by wiring. A change lives inside an action, which lives inside a resource; a trigger lives inside a resource’s
obanblock. Spray has “no analog of a sub-module or sub-component” (§2.2). The composition an ALA app wants, this feature’s store wired to that feature’s input by the layer above, has no place in the DSL. The DSL’s place for cross-cutting behaviour is the resource.
So Ash is best read as a very good implementation of the architecture ALA replaces. That doesn’t make it unusable. It makes the usable slice narrow and specific.
Where each piece sits
| Ash piece | ALA reading | Layer, and the rule |
|---|---|---|
Ash.Resource with attributes and a data layer | an abstraction of persistence: one table and its schema (§6.19) | Foundation, like a Repo plus a schema |
one resource per feature’s private data, keyed by an identity (cart_items with cart_id) | Spray’s identity-key technique (§6.17.2) | Foundation; owned by that feature, configured into it by the composition |
one Order resource every feature reads and writes, with attributes for each use case | the shared entity: two features knowing the meaning of the same data (R10), each reaching it by name (R1, R9) | the defect; split by use case, share the key |
relationships across features’ resources, and loading through them | an association that lets one feature walk into another’s data (R10) | avoid across features; inside one feature’s resources it’s that feature’s own joins |
actions with changes and validates | the feature’s rules, when the resource is that feature’s; a shared rule stuffed into a shared entity, when it isn’t | per-feature: Foundation store with its intrinsic rules; shared: R10, R6 |
validation messages in the resource (message: "...") | words below the composition (R3) | pass them in: Ash takes messages as options, and a form can supply them |
calculations, aggregates | pure rules over the resource’s own data: fine; an aggregate across features’ resources is a shared read (R10) | with the resource it belongs to |
identities, multitenancy | intrinsic to the table | Foundation |
policies (Ash.Policy.Authorizer) | an intrinsic constraint of the data (who may read a table) or a product rule (who may check out)? Usually the latter: R3 and R11 when the app’s rules live in a resource | keep the intrinsic ones; pass product rules in as configured checks |
Ash.Domain and code interfaces (define) | the main interface of a store: configuration-shaped calls from the layer above (§2.3.4). A problem only when features call peers’ domains through it | Foundation entry point |
AshPhoenix.Form | a generic record form the page configures and wires: a UI domain abstraction host | Domain UI |
AshStateMachine | a state machine inside the entity: history belongs in a state-machine abstraction (§4.16), which this is, but attached to the hub | fine for a per-feature resource; a shared resource’s state machine is a shared rule |
Ash.Notifier, Ash.Notifier.PubSub | the resource publishing what it did: see below | — |
AshOban triggers and scheduled actions | the resource scheduling its own follow-ups: see below | — |
Reactor | a saga runner: an execution model | Programming Paradigms, if used generically |
The rule in one sentence: use Ash.Resource as the store abstraction for one feature’s private table, keyed by an identity, with that feature’s intrinsic rules inside it, and nothing more. Attributes, a data layer, per-feature actions and calculations, identities, code interfaces and the form are the parts that fit. Relationships across features, a resource every feature reads, policies encoding product rules, and the two mechanisms below are the parts that don’t, each with a named rule against it.
The honest caveat: Ash’s power is in exactly the parts ALA forbids. Cross-resource loads, relationships, notifiers that fan out, triggers that react to data. An Ash-in-ALA app uses a small and unusual slice, and a team that chose Ash for its derivations will feel it fighting them. If the team wants the derivations, they are choosing the entity architecture, and the checklist’s job is to say so, not to pretend the two agree.
Notifications, the PubSub notifier, and AshOban
These three are where Ash does the most for you and where ALA’s rules bite hardest, because each is a lower abstraction deciding what happens next. Spray’s rule for that is §4.4.2: “receivers never register themselves to a sender, or to a public event”, and its mirror, an output port “may say ‘This has happened’ or ‘Here is my result’, not ‘do this next’” (§7.2.6). The LiveView variants settled the same question for plain Phoenix: broadcasts are wired by the page, never fired from an Ecto callback, and a topic is a global event name owned by one module.
Ash notifications
After an action commits, Ash builds an Ash.Notifier.Notification (the resource, the action, the record, the changeset) and hands it to every notifier the resource declares. Two ways to receive it:
- Returned to the caller. With
return_notifications?: true, the action answers{:ok, record, notifications}instead of sending anything. The caller decides. This is the checklist’s “return an outcome instead of calling a peer”, and it is the ALA form: the page (or the feature that holds the store as configuration) receives the fact and routes it through its own wiring, exactly as it routes a feature’s{:removed, item}. The resource knows no destination. Use this. - A notifier module.
notifiers: [MyNotifier]on the resource, andMyNotifier.notify/1runs after commit. The resource names the module. That is a knowledge dependency downward only if the notifier is generic: a ten-line module in Programming Paradigms that does nothing but hand the notification to whatever the application registered at start-up (a registry entry, a configured{module, function}, a PubSub topic read from application config). ThenAsh.Notifieris the paradigm interface and the notifier is a port the composition wires, which R9 allows. A notifier that itself decides to email, broadcast on a topic it names, or update another resource is the §4.4.2 defect, however tidy. Use only as a generic forwarder; wire the receiver in the application.
Ash’s PubSub notifier
Ash.Notifier.PubSub is the second form with the destination written in the resource:
pub_sub do
module MyAppWeb.Endpoint
prefix "cart"
publish :update, ["item", :cart_id]
end
The resource names the endpoint, the prefix and the topic shape, and every page that subscribes to "cart:item:42" agrees on that string with the resource. That is both halves of the smell at once: the resource is the sender choosing its receivers’ channel (§4.4.2), and the topic is a symbolic connection two modules agree on (§4.7.4, R5). It is also exactly as convenient as it looks, which is why the LiveView variants had to find a replacement they could live with.
The replacement is the same as plain Phoenix’s: the composition owns the topic. Either return the notification to the caller and let the page broadcast through the Broadcast module that owns the topic (V50, V51), or configure a generic notifier with the topic from application config so the resource never writes it. In both, one module owns publish and subscribe, the page subscribes through an on_mount hook, and handle_info is a wire to a feature’s input. What you give up: the resource no longer fans out on its own, so a second page that wants the fact has to be wired to it. What you get: the wiring diagram shows every fan-out, which was the point.
AshOban
AshOban attaches two things to a resource. A trigger declares a scheduled scan (trigger :process do where expr(status == :pending); action :process end): every so often, find records matching the condition and run the action on each. A scheduled action declares a cron (scheduled_action :daily_digest, "0 8 * * *") that runs a generic action. Both put knowledge in the resource that belongs above it:
- the condition “pending orders get processed” is a requirement, a product’s rule about what happens next, written in the entity (R11’s history and follow-ups; R1’s lower abstraction deciding the next step);
- the cron is an application literal below the composition (R3), of exactly the kind the checklist’s R3 bullets hoist (“a free-shipping threshold of 100, a currency, a label”);
- the trigger’s
actionnames what to run, which is the resource calling itself, which is fine, or another resource’s action through a change, which is a peer edge.
The ALA shape keeps Oban as the execution model (Programming Paradigms: a job runner is “how a kind of connection runs”) and moves the declarations up:
- Cron belongs in the application’s Oban configuration (
config :my_app, Oban, plugins: [{Oban.Plugins.Cron, crontab: [{"0 8 * * *", MyApp.Jobs.Digest}]}]), which is the composition: application config read by the top layer. The job module is one wire: it builds or finds the feature and pushes one input, the way the Rails variants’PushLaterrebuilds a screen and pushescharge. - A “find pending records and process each” trigger becomes a feature’s
loadinput wired to a store, run by a job the application scheduled. The condition is the feature’s; the schedule is the application’s; the resource knows neither. AshOban’s trigger is defensible for one case: a condition intrinsic to the resource’s own data lifecycle that no product would change (a retry of a failed delivery the resource itself records). Even there the schedule should come from configuration, and the checklist’s R3 test applies: would another product using this resource want the same cron?
A layer map for an Ash app
With the pieces placed, the linter’s map is short:
def layers do
[
{:app, [~r/Web\..*Live$/], paths: [~r{/live/}]},
{:feature, [~r/\.Features\./]},
{:domain, [~r/\.Domain\./, ~r/Web\.Components\./]},
{:paradigms, [~r/\.Paradigms\./, ~r/Notifier$/, ~r/\.Jobs\./]},
{:store, [], uses: [~r/Ash\.Resource/, ~r/Ash\.Domain/], peer_ok: true}
]
end
A resource lands in the bottom layer by its use, wherever its file sits. Then the linter sees what it can see: a feature referencing another feature’s resource is R1 (and R10 once two features read one); a resource module referencing a notifier that reaches up to a page is an upward edge; a topic string in a resource and a page is R5. What it can’t see is the DSL’s own containment, relationships declared as atoms, a policy’s product rule, or a cron in a scheduled_action. Those stay a reader’s.
So, should an ALA app use Ash?
For the store abstraction, yes, and it is a good one: a resource with attributes, a data layer, intrinsic validations and a code interface is cleaner than a hand-written Repo module, and AshPhoenix.Form is a ready domain UI abstraction. For everything Ash derives from the resource across resources, no: relationships between features, notifiers that name their destination, the PubSub notifier’s topics, triggers that encode what happens next. Each of those has an ALA form that is more code and more explicit, and the explicitness is what the checklist is for. Neither variant corpus has an Ash app yet; a V51B built this way would be the honest test of whether the narrow slice is worth the dependency.