What is the benefit of using the Ash Framework? Why can’t one just use Phoenix or Phoenix LiveView?
You would generally use it with Phoenix and LiveView, not instead of. It takes care of a lot of the common application requirements (auth, notifications etc) through a declarative DSL.
We have some documentation along these lines here: What is Ash? — ash v3.4.71
And this thread has a lot of various takes on Ash, started by someone who decided (or was considering the decision) on not using it: My thoughts on Ash
You can think of Ash as a way to build up the contexts that you then use with Phoenix or LiveView. Ash doesn’t replace Phoenix, it works alongside it.
So if you’re building a todo app, for example, you might have a traditional Phoenix context like this:
defmodule MyApp.Todos do
import Eco.Query
alias MyApp.Todos.TodoList
def get_todos(user_id) do
from(t in TodoList, where tl.user_id == ^user_id)
|> Repo.all()
end
def create_todo(user_id, params)
# ...
end
def create_todo_changeset(params)
# ...
end
def delete_todo(id)
# ...
end
# a ton of other methods that build up over time
# such as maybe you want to filter by date, or status, etc.
end
And then in your LiveView, you’d call into MyApp.Todos.get_todos
somewhere in your assigns.
Well, wish Ash, you’d just model that resource, and let Ash derive all those context functions for you.
defmodule MyApp.Todos.TodoList
actions do
defaults [:read, :update, :create, :destroy]
default_accepts [:title, :description]
end
# attributes, etc.
end
Then you’d define your domain
defmodule MyApp.Todos
resources do
resource MyApp.Todos.TodoList
define :create_list, action: :create
define :get_todos, action: :read
define :get_todo, action: :read, get_by: :id
end
end
end
Then Ash sets up all those actions/functions for you. You’d then call that instead of your context when setting up your assigns:
{:ok, socket |> assign(:todo_lists, MyApp.Todos.get_todos()}
It’s extremely powerful, because those read functions, for example, automatically provide support for filtering, authentication, sorting, etc. All things you’d need to build up manually by hand in your Phoenix context.
It does take some time for it to click, but I find that once you do, it can be extremely productive to start using.
My favourite thing in Ash ( and especially in Spark ) are the possibilities of Introspection.
Building extensions in Ash is great and I’m basically creating a suite of extension to integrate Commanded and generate our backoffice directly from my resources.
The whole DSL / Transformer / Verifiers / Info structure is great once you start understanding how it works. I think Spark is the jewel of the whole thing.