`<project_name>.ex` - it's purpose?

Hey, I’ve already written few apps in Phoenix, but I started thinking about one special file which is generated after creating new project, <project_name>.ex . What’s it’s purpose? <project_name>_web.ex contains macros helping setting up views, templates, controllers and so on. But I don’t see analogy in <project_name>.ex, description also isn’t helping much:

<project_name> keeps the contexts that define your domain
and business logic.

Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.

If I want to access accounts context I use AppName.Accounts, if I want to use context connected with web I use AppNameWeb.ContextName. But how should I use plain AppName or AppNameWeb?

Personally I remove app_name_web.ex and replace all it occurrences with direct imports in controllers and views. I do not like the *_web.ex idea and prefer to be explicit about imports.

If it comes about app_name.ex if it is small project then I often implement all API for the frontend there. For example my imager project. You can see that I have changed few more Phoenix naming conventions, namely I use AppName.Controllers.Name without Controller suffix, the same for the views.

2 Likes

If your app was being used outside of the web context, such as in an Umbrella app, the <project_name>.ex file would be an entry point into your apps functionality.

This convention is perhaps more suitable for libraries who have an external interface, rather than a self-contained phoenix application.

3 Likes