Best Practices: Code Interfaces vs Resource Actions
Hi, everyone! I have one question: Should we use domain code interfaces from within resource actions? what are the best practices regarding when to use each of them?
I'm using code interfaces from Phoenix, but would like to know whether I should be using them from within resource actions too. I think it looks cleaner, but it feels weird to call the "outer" layer from an "inner" layer
4 Replies
Hi @Iuri Madeira ,
I was reading the documentation regarding this and I think this is the closest thing I could find:
When to use actions vs code interfaces: - Actions define what operations are possible on your resource - Code interfaces provide function-based access to those actions - Actions don't require code interfaces and can be used by extensions like AshJsonApi - Code interfaces make actions callable as functions (e.g., DashboardGroup.get_by_id/1) This pattern encourages proper separation of concerns by keeping domain logic in your resources and providing clean interfaces for your web layer.Maybe there is a better resource for this, idk. Reading this as an entry/enthusiast level Elixirist, I'm wondering whether the use of code interfaces in implementations of changes for instance would have implications for the JSON API. Judging by the thumbs up you've gotten I'd guess that it doesn't, but I'd be curious to know more. https://hexdocs.pm/ash/code-interfaces.html#using-the-code-interface
Solution
Its fine if you call the public interface from the internals IMO.
I know some folks will define code interfaces on resources as a sort of "internal domain-private interface"
and use those from other resources
code interfaces on resources are an interesting take! Thanks, Zach!