跳到主要内容
知仓学习社ZHICANG

phoenix-contexts

Phoenix context design — creating/splitting contexts, Scope (1.8+), Ecto.Multi,

不碰外部(只输出文字)无严重或高危命中oliver-kriska/claude-elixir-phoenix

它会碰到什么

扫了多少6 个文本文件,27 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Phoenix Contexts Reference

> Ash projects: Ash.Domain replaces Phoenix contexts for data access — use the ash-framework skill. Context boundary and PubSub patterns still apply.

Reference for designing and implementing Phoenix contexts (bounded contexts).

Iron Laws — Never Violate These

  1. CONTEXTS OWN THEIR DATA — Never query another context's schema directly via Repo
  2. SCOPES ARE MANDATORY (Phoenix 1.8+) — Every context function MUST accept scope as first parameter
  3. THIN CONTROLLERS/LIVEVIEWS — Controllers translate HTTP, business logic stays in contexts
  4. NO SIDE EFFECTS IN SCHEMAS — Use Ecto.Multi for transactions with side effects

Context Structure

lib/my_app/
├── accounts/           # Context directory
│   ├── user.ex         # Schema
│   ├── scope.ex        # Scope struct (Phoenix 1.8+)
├── accounts.ex         # Context module (public API)

Phoenix 1.8+ Scopes (CRITICAL)

All context functions MUST accept scope as first parameter:

def list_posts(%Scope{} = scope) do
  from(p in Post, where: p.user_id == ^scope.user.id)
  |> Repo.all()
end

def create_post(%Scope{} = scope, attrs) do
  %Post{user_id: scope.user.id}
  |> Post.changeset(attrs)
  |> Repo.insert()
  |> broadcast(scope, :created)
end

Quick Decisions

When to SPLIT contexts?

  • Module exceeds ~400 lines
  • Functions don't share domain language
  • Could theoretically be a separate microservice
  • Team member could own it independently

When to KEEP together?

  • Resources share vocabulary and domain concepts
  • Functions frequently operate on same data together
  • Splitting would create excessive cross-context calls

Cross-Context References

# ✅ Reference by ID, convert at boundary
def create_order(%Scope{} = scope, user_id, product_ids) do
  with {:ok, user} <- Accounts.fetch_user(scope, user_id) do
    do_create_order(scope, user.id, product_ids)
  end
end

# ❌ Reaching into other context's internals
alias MyApp.Accounts.User  # Don't do this
Repo.all(from o in Order, join: u in User, ...)  # Don't query other schemas

Anti-patterns

| Wrong | Right |

|-------|-------|

| Service objects (UserCreationService) | Context functions (Accounts.create_user/2) |

| Repository pattern wrapping Repo | Repo IS the repository |

| Direct Repo calls in controllers | Delegate to context |

| Schema callbacks with side effects | Use Ecto.Multi |

Version Notes

  • Phoenix 1.8+: Uses built-in %Scope{} struct for authorization context
  • Phoenix 1.7: Requires manual authorization context (see references/scopes-auth.md "Pre-Scopes Patterns")

References

For detailed patterns, see:

  • references/context-patterns.md - Full context module, PubSub, Multi, cross-boundary
  • references/scopes-auth.md - Scope struct, multi-tenant, authorization, plugs
  • references/routing-patterns.md - Verified routes, pipelines, API auth
  • references/plug-patterns.md - Function/module plugs, placement, guards
  • references/json-api-patterns.md - JSON controllers, FallbackController, API auth

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 553
本站分层T2
该仓技能数322
原文件路径targets/amp/skills/phoenix-contexts/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 322 个技能

同名技能的其他版本

有 6 个不同仓库或目录里都有叫 phoenix-contexts 的技能。它们内容并不相同,别混用: