shankardevy
The following code present in my controller file generated by phx.gen.html call for clarity on the intended usage of context and schema.
def index(conn, _params) do
invoices = Billing.list_invoices()
render(conn, "index.html", invoices: invoices)
end
def new(conn, _params) do
changeset = Billing.change_invoice(%InvoiceApplication.Billing.Invoice{})
render(conn, "new.html", changeset: changeset)
end
Except for the new action, where the schema %InvoiceApplication.Billing.Invoice{} is used, no part of the controller directly calls the schema. If the intention of context module is to provide public apis and hide the implementation details such as the schema name, phx.gen.html should generate a controller without specifying schema names such as Billing.new_invoice/0 or Billing.build_invoice/0.
So the clarity needed is do we expose schema outside the context or not? If we do expose them, then clearly context file is not the only public api of a context.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
We do expose them outside the context. After all,
Billing.list_invoice/0returns a list of invoice structs!shankardevy
There are a few related concerns with the current
change_invoice/2code used innewaction of controller.change_invoice/2doesn’t say my intention clearly. I am not changing an existing invoice, rather trying to build a new one. So a function name likebuild_invoice/1ornew_invoice/1is more clearly giving the intention. The arity in this new function isattrsof the currentchange_invoice/2and defaults to%{}.You mentioned that
Billing.list_invoice/0returns invoice structs so it’s ok to use invoice struct directly in thenewaction. However, is it not an unnecessary input to provide? Because, I am in the new action, calling an invoice related function on my billing context and I expect an invoice struct, without having me to say that again? I have already been explicit in conveying my intention. For eg.,Billing.get_invoice!(id)doesn’t require me to input%Invoice{}.Thanks for your time and input.
PatNowak
Afaik: Contexts are layer of abstractions that helps you group your business logic code. It’s just additional middleware that controller should ask for your data, not directly repo, but still you receive data- structs related to certain schema modules.
Another benefit of using contexts is that they group your schemas visually- this is the point which was mentioned by a Chris- you see which contexts refer to which schemas.
From now on: controller shouldn’t talk with repo, but with contexts.
radar
Do you have the code for this app or an example of what commands you ran to generate it? I’m keen to look at it and read through it.
shankardevy
I just ran the Phoenix 1.3 generator code like this
These concepts are clear @PatNowak. My question is should we pass the struct explicitly to the context function in
newaction of the generated controller.PatNowak
I wouldn’t. All you need to edit existing one is id, for new struct you only pass params and context should internally call function with empty struct for repo
insert.shankardevy
That’s exactly what I am proposing here.
mix phx.gen.htmlproduces the below code in thenewaction of controller.I am proposing that it should be
PatNowak
It’s looks nice. I assume that context is keeps proper level of isolation between controller and repo & schemas. It would be logicall to hide informations about structures inside context function calls:
Keeping this information about
Invoiceis redundant for controller - I already wants to change invoice, so I only wants to deal with these kind of structures.