4 ms·
Hello, article author here. Can you expand on what you mean by top-level module? My understanding of contexts (from the docs) is that they are intended to "...
by monooso 4y ago
Hello, article author here. Can you expand on what you mean by top-level module?
My understanding of contexts (from the docs) is that they are intended to "...encapsulate data access and data validation." They're intended to be the layer of your application that talks to the database. Do you have a different approach?
- sodapopcan 4y agoI _think_ they are talking about treating the top level module as the facade for the whole application, which is something I've thought about doing a bunch. IE, it would be full of delegates to all your contexts and then _every_ public function is available off of it. I don't think this is a terrible idea but I like to draw boundaries and say "It's ok to reach one level deep from any point in the context tree". ie App.Chat.send_message/2 is cool, but MyApp.Chat.Messages.send_message/2.
- beckler 4y agoNot OP, but I think he might be hinting towards something like this: https://learn-elixir.dev/blogs/creating-reusable-ecto-code https://learn-elixir.dev/blogs/creating-reusable-ecto-code
- monooso 4y agoThe article you linked to reminds me of Chris Bailey's "compositional" Ecto query patterns post, which is worth a read if you haven't seen it before: https://cbailey.co.uk/posts/compositional_elixirecto_query_patterns https://cbailey.co.uk/posts/compositional_elixirecto_query_p...
- zachallaun 4y agoI don’t know what the person you’re responding to is getting at, but I found the article very useful! Exposing the changeset through the context is also useful in LiveViews when dealing with forms — once the changeset is valid, calling the relevant function with the same attrs should succeed. Re: the sibling comment referring to queries, nothing you’re doing in the article prevents that. Ecto’s changesets are often an unfamiliar concept, so I wonder if that’s where some confusion is coming from.
- nesarkvechnep 4y agoI like to use changesets for form validation, for example, but those changesets are different than the ones I use in my public API functions. Most of the time what I present to the user don’t map 1:1 to my Ecto schemas and it doesn’t have to.
- gregors 4y agoThis is going to be an unpopular opinion, but I think it's unfortunate that changesets are tied to the views in Phoenix. It's major leakage of ecto and the inner domain at best and at worst it intertwines various concerns - validation and what's changing. The naming of it gives that design choice away. It it didn't it would probably have a name with "validation" in it somewhere. I'd much rather have domain structs implement a protocol. Maybe that's possible I haven't really looked into it. However, what you say about having different changesets for validation seems to make a ton of sense to me and has given me some ideas. Thanks.
- monooso 4y agoThanks, I'm glad you found it useful.