Hacker News new | past | comments | ask | show | jobs | submit login

The directory reorganization is a good sign that Elixir/Phoenix are not simply rehashing Ruby/Rails for the sake of popularity but are willing to prioritize what makes the most sense for this particular language - and asking how they can improve upon what already exists.

I recently ported my app from 1.2->1.3 and moving away from Models to Contexts/Data was a simple transition that makes a lot of sense.

The `data` files (aka your new models) are basically where the schema for your model lives and the `context` (your models API) is the interface for your data.

For example when building a blog:

Instead of having User, Session, Post, and Comment models which contains your DB schema, business logic, and interfaces for getting/setting data all models directory ala Rails:

    blog/app/models/comment.rb
    blog/app/models/post.rb
    blog/app/models/session.rb
    blog/app/models/user.rb
    blog/app/controllers/...
    blog/app/views/...
you instead create a namespace for each group of data:

    lib/blog_app/accounts/accounts.ex
    lib/blog_app/accounts/session.ex
    lib/blog_app/accounts/user.ex
    lib/blog_app/blog/blog.ex
    lib/blog_app/blog/comment.ex
    lib/blog_app/blog/post.ex
    lib/blog_app/web/controllers/...
    lib/blog_app/web/views/...
And in your data file `lib/blog_app/blog/post.ex` for example, you'd keep just your schema defining the fields like "title, permalink, body, etc" and code to handle validations and virtual attributes.

Then in your context file `lib/blog_app/blog/blog.ex` you define the API that access your data. So from your controller instead of calling:

    Post.all
    Comment.all
    Comment.find(1)
    User.new({..})
You now call:

    Blog.list_posts
    Blog.list_comments
    Blog.get_comment(1)
    Accounts.create_user({..})
It makes for a very logical structure for your MVC code.



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: