shankardevy

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.

Showing Posts 1 to 8

josevalim

josevalim

Creator of Elixir

We do expose them outside the context. After all, Billing.list_invoice/0 returns a list of invoice structs!

shankardevy

shankardevy OP

There are a few related concerns with the current change_invoice/2 code used in new action of controller.

  1. change_invoice/2 doesn’t say my intention clearly. I am not changing an existing invoice, rather trying to build a new one. So a function name like build_invoice/1 or new_invoice/1 is more clearly giving the intention. The arity in this new function is attrs of the current change_invoice/2 and defaults to %{}.

  2. You mentioned that Billing.list_invoice/0 returns invoice structs so it’s ok to use invoice struct directly in the new action. 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

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

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

shankardevy OP

I just ran the Phoenix 1.3 generator code like this

  mix phx.gen.html Billing Invoice invoices due_date:date ...

These concepts are clear @PatNowak. My question is should we pass the struct explicitly to the context function in new action of the generated controller.

PatNowak

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

shankardevy OP

That’s exactly what I am proposing here. mix phx.gen.html produces the below code in the new action of controller.

changeset = Billing.change_invoice(%InvoiceApplication.Billing.Invoice{})

I am proposing that it should be

 changeset = Billing.new_invoice  # or build_invoice and optionally pass attrs to it.
PatNowak

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:

changeset = Billing.change_invoice(id, params)
# and it would call
Invoice.changeset(invoice_struct, params)
# where invoice_struct is
Repo.get!(Invoice, id)

Keeping this information about Invoice is redundant for controller - I already wants to change invoice, so I only wants to deal with these kind of structures.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
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 Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews