Domain Driven Designのロゴ

Domain Driven Design

コミュニティ
fatonh
domain-driven-design

Use when modeling a domain in Symfony — aggregates, entities, value objects, domain events, repositories, and the ubiquitous language. Use when the task mentions DDD, aggregate, value object, domain event, or invariant.

概要

公開者fatonh
リポジトリsymfony-skills
スキル名domain-driven-design
スター
36
フォーク
3
同梱ファイル
指示のみ
ライセンスMIT
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    fatonhがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキルDomain Driven DesignをTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/fatonh/symfony-skills/tree/main/skills/domain-driven-design
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/fatonh/symfony-skills.git /tmp/symfony-skills
mkdir -p .claude/skills
cp -r /tmp/symfony-skills/skills/domain-driven-design .claude/skills/domain-driven-design
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでDomain Driven Designを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあると、モデルが自動的にDomain Driven Designを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Domain Driven Designは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Domain-Driven Design (Symfony)

Building blocks

Tactical patternIn Symfony/PHP
Aggregate rootEntity class that guards invariants and is the only entry point to its cluster
EntityHas identity (OrderId), mutable over its lifecycle
Value objectfinal readonly class, no identity, compared by value (Money, Email)
Domain eventfinal readonly class recorded on the aggregate, dispatched after persist
RepositoryInterface in the domain, Doctrine adapter in infrastructure (see hexagonal-architecture)
Domain serviceStateless logic that doesn't belong to a single aggregate

Value objects

Immutable, self-validating, compared by value. Replace primitive obsession.

php
// ✅ GOOD — value object enforces its own invariants
final readonly class Email
{
    public function __construct(public string $value)
    {
        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
            throw new \InvalidArgumentException("Invalid email: {$value}");
        }
    }

    public function equals(self $other): bool
    {
        return $this->value === $other->value;
    }
}

final readonly class Money
{
    public function __construct(public int $amountInCents, public Currency $currency) {}

    public function add(self $other): self
    {
        if ($this->currency !== $other->currency) {
            throw new \DomainException('Currency mismatch');
        }
        return new self($this->amountInCents + $other->amountInCents, $this->currency);
    }
}

// ❌ BAD — primitives everywhere, no invariants, validation scattered across services
function createOrder(string $email, int $totalCents): void { /* ... */ }

Map value objects with Doctrine embeddables (#[ORM\Embedded]) or a custom DBAL type, so the table stays flat while the model stays rich.

Aggregates protect invariants

  • The aggregate root is the only object outside code may hold a reference to.
  • Mutations go through behavior methods on the root, never setters.
  • Invariants are checked inside those methods — an aggregate can never be in an invalid state.
  • Keep aggregates small; reference other aggregates by ID, not object reference.
php
// ✅ GOOD — behavior methods guard invariants; children reached only through the root
#[ORM\Entity]
class Order
{
    #[ORM\Id, ORM\Column(type: 'uuid')]
    private Uuid $id;

    #[ORM\Column(enumType: OrderStatus::class)]
    private OrderStatus $status;

    /** @var Collection<int, OrderLine> */
    #[ORM\OneToMany(targetEntity: OrderLine::class, mappedBy: 'order', cascade: ['persist'], orphanRemoval: true)]
    private Collection $lines;

    /** @var DomainEvent[] */
    private array $recordedEvents = [];

    private function __construct(Uuid $id)
    {
        $this->id = $id;
        $this->status = OrderStatus::Pending;
        $this->lines = new ArrayCollection();
    }

    public static function place(): self
    {
        $order = new self(Uuid::v7());
        $order->record(new OrderPlaced($order->id));
        return $order;
    }

    public function addLine(Uuid $productId, int $quantity): void
    {
        if ($this->status !== OrderStatus::Pending) {
            throw new \DomainException('Cannot modify a confirmed order'); // invariant
        }
        $this->lines->add(new OrderLine($this, $productId, $quantity));
    }

    public function confirm(): void
    {
        if ($this->lines->isEmpty()) {
            throw new \DomainException('Cannot confirm an empty order'); // invariant
        }
        $this->status = OrderStatus::Confirmed;
        $this->record(new OrderConfirmed($this->id));
    }

    /** @return DomainEvent[] */
    public function releaseEvents(): array
    {
        $events = $this->recordedEvents;
        $this->recordedEvents = [];
        return $events;
    }

    private function record(DomainEvent $event): void
    {
        $this->recordedEvents[] = $event;
    }
}

// ❌ BAD — anemic entity: public setters let any caller break invariants
class Order
{
    public OrderStatus $status;
    public Collection $lines;
    public function setStatus(OrderStatus $s): void { $this->status = $s; }
}

Domain events

Record events inside the aggregate; dispatch them after the transaction commits so subscribers never see uncommitted state. A Doctrine postFlush listener or Messenger does the dispatch.

php
// ✅ Dispatch recorded events after flush via a Doctrine event subscriber
#[AsDoctrineListener(event: Events::postFlush)]
final class DispatchDomainEventsListener
{
    public function __construct(private readonly MessageBusInterface $eventBus) {}

    public function postFlush(PostFlushEventArgs $args): void
    {
        $uow = $args->getObjectManager()->getUnitOfWork();
        foreach ($uow->getIdentityMap() as $entities) {
            foreach ($entities as $entity) {
                if ($entity instanceof RecordsEvents) {
                    foreach ($entity->releaseEvents() as $event) {
                        $this->eventBus->dispatch($event);
                    }
                }
            }
        }
    }
}

Ubiquitous language

  • Name classes and methods with the business vocabulary: Order::confirm(), not Order::setStatusToConfirmed().
  • The same term means the same thing in code, tests, and conversation.
  • Bounded contexts get their own namespace (App\Sales\…, App\Billing\…); the same word may differ between them.

Gotchas

  • Agent generates anemic entities with public setters — model behavior methods that enforce invariants.
  • Agent uses primitive string/int for Email, Money, OrderId — introduce value objects.
  • Agent references other aggregates by object (#[ORM\ManyToOne] private Customer $customer) — reference by ID across aggregate boundaries.
  • Agent dispatches domain events before flush() — record on the aggregate, dispatch after commit.
  • Agent puts cross-aggregate logic on an entity — use a stateless domain service.
  • Agent makes huge aggregates loading dozens of children — keep them small; consider a separate read model.
  • Agent names methods after database operations (save, update) instead of intent (confirm, cancel).

よくある質問

AIスキル「Domain Driven Design」では何ができますか?

Use when modeling a domain in Symfony — aggregates, entities, value objects, domain events, repositories, and the ubiquitous language. Use when the task mentions DDD, aggregate, value object, domain event, or invariant.

TypingMindでDomain Driven Designを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Domain Driven Designはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにDomain Driven Designをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/fatonh/symfony-skills/tree/main/skills/domain-driven-designを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Domain Driven Designを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストが合致したときにこのスキルを読み込めます。

Domain Driven Designは何個のAIモデルで使えますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもDomain Driven Designを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Domain Driven Design」は無料ですか?

はい。fatonhがGitHubでMITライセンスの下で公開しています。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇