【问题标题】:What is context in elixir phoenix html generatorelixir phoenix html生成器中的上下文是什么
【发布时间】:2017-02-25 22:00:28
【问题描述】:

我想看看凤凰1.3-dev。我从repository 克隆了phoenix,并按照phoenix/installer/README.md 构建存档。

phoenix/installer/README.md(供参考)

$ cd installer
$ MIX_ENV=prod mix archive.build
$ mix archive.install

我通过mix phx.new blog 创建了一个新的phoenix 应用程序,但是当我尝试通过mix phx.gen.html Post posts title body:text 创建一个简单的html 脚手架时,我收到以下错误消息:

** (Mix) Expected the schema argument, ":\"Elixir.Company.companies\"", to be a valid module name

mix phx.gen.html and mix phx.gen.json expect a context module name,
followed by singular and plural names of the generated resource, ending
with any number of attributes:

    mix phx.gen.html Accounts User users name:string
    mix phx.gen.json Accounts User users name:string

The Accounts context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Blog.User).

我的问题是为什么要添加context 模块?它如何以及在哪里有帮助? 如果您对此有所了解,请描述一个用例以便我理解。

【问题讨论】:

  • 错误消息本身是不言自明的。业务逻辑中的“实体”不应 100% 精确映射到数据库模型。

标签: elixir phoenix-framework


【解决方案1】:

上下文模块允许我们将 Repo 调用从控制器移除到它们自己的模块,这样就可以分离关注点。

如需了解更多信息,请尝试收听 Chris McCord 的演讲:

https://www.youtube.com/watch?v=tMO28ar0lW8&index=1&list=PLE7tQUdRKcyaMEekS1T32hUw19UxzqBEo

并查看 Phoenix 代码中的 cmets:

https://github.com/phoenixframework/phoenix/blob/07f1bbc7627117adc23ec8355a88465859e4302d/lib/mix/tasks/phx.gen.context.ex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多