create-unit-of-work

Generate Unit of Work pattern components for PHP 8.4, creating infrastructure with transactional consistency that includes aggregate tracking, flush/rollback, domain event collection, and unit tests.

Last repository push
GitHub Stars
109
Forks
26
Issues / PRs
1

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "create-unit-of-work" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-107a742b2b413041fef883bb163af366d615d60d4b9929ed3b7ebc3e445ffc72. Verify the upstream source and version against https://github.com/dykyi-roman/awesome-claude-code before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Generate Unit of Work pattern components for PHP 8.4, creating infrastructure with transactional consistency that includes aggregate tracking, flush/rollback, domain event collection, and unit tests.

0 Downloads

Introduction source: third_party · dykyi-roman · 0.0.1 Reference

SkillPi interpretation

Tasks
Generates Unit of Work pattern components for PHP 8.4. Creates transactional consistency infrastructure with aggregate tracking, flush/rollback, domain event collection, and unit tests.
Audience
| Scenario | Example | |----------|---------| | Multi-aggregate transactions | Order + Payment + Inventory in single transaction | | Batch persistence | Flush multiple entity changes at once | | Change tracking | Detect dirty entities for selective updates | | Domain event collection | Collect and dispatch events after successful commit | | Repository coordination | Ensure all repositories share the same transaction |
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: create-unit-of-work
description: Generates Unit of Work pattern components for PHP 8.4. Creates transactional consistency infrastructure with aggregate tracking, flush/rollback, domain event collection, and unit tests.

Unit of Work Generator

Creates Unit of Work pattern infrastructure for transactional consistency across multiple aggregates.

When to Use

Scenario Example
Multi-aggregate transactions Order + Payment + Inventory in single transaction
Batch persistence Flush multiple entity changes at once
Change tracking Detect dirty entities for selective updates
Domain event collection Collect and dispatch events after successful commit
Repository coordination Ensure all repositories share the same transaction

Component Characteristics

UnitOfWorkInterface

  • Application layer port
  • begin(), commit(), rollback() transaction methods
  • registerNew(), registerDirty(), registerDeleted() tracking methods
  • flush() to persist all tracked changes
  • collectEvents() from tracked aggregates

UnitOfWork

  • Infrastructure implementation (PDO/Doctrine-based)
  • Identity Map for tracked entities
  • Dirty checking for change detection
  • Ordered persistence (inserts → updates → deletes)
  • Wraps all operations in database transaction

AggregateTracker

  • Tracks entity state (NEW, CLEAN, DIRTY, DELETED)
  • Identity Map prevents duplicate loading
  • Computes changeset on flush

TransactionManagerInterface

  • Abstracts transaction lifecycle
  • Supports nested transactions (savepoints)
  • Domain layer contract

DomainEventCollector

  • Collects events from all tracked aggregates
  • Dispatches events AFTER successful commit
  • Clears events on rollback

Generation Process

Step 1: Analyze Request

Determine:

  • Context name (Order, Payment, Inventory)
  • Which aggregates participate in unit of work
  • Event dispatcher integration (Symfony/custom)

Step 2: Generate Core Components

Create in this order:

  1. Domain Layer (src/Domain/Shared/UnitOfWork/)

    • EntityState.php — State enum (New, Clean, Dirty, Deleted)
    • TransactionManagerInterface.php — Transaction contract
    • DomainEventCollectorInterface.php — Event collection contract
  2. Application Layer (src/Application/Shared/UnitOfWork/)

    • UnitOfWorkInterface.php — Main port
    • AggregateTracker.php — Identity map and change tracking
  3. Infrastructure Layer (src/Infrastructure/Persistence/UnitOfWork/)

    • DoctrineUnitOfWork.php — Doctrine-based implementation
    • DoctrineTransactionManager.php — Doctrine transaction manager
    • DomainEventCollector.php — Event collector with dispatcher
  4. Tests

    • EntityStateTest.php
    • AggregateTrackerTest.php
    • DoctrineUnitOfWorkTest.php

Step 3: Generate Context-Specific Integration

For each context (e.g., Order):

src/Application/{Context}/
└── {Context}UnitOfWorkAware.php (trait or base class)

File Placement

Layer Path
Domain Types src/Domain/Shared/UnitOfWork/
Application Port src/Application/Shared/UnitOfWork/
Infrastructure src/Infrastructure/Persistence/UnitOfWork/
Unit Tests tests/Unit/{Layer}/{Path}/

Key Principles

Transaction Boundaries

  1. Begin transaction at use case entry
  2. Track all aggregate changes within boundary
  3. Flush all changes atomically
  4. Dispatch domain events after successful commit
  5. Rollback clears all tracked changes

Identity Map

  1. One entity instance per identity in memory
  2. Prevent duplicate loads from database
  3. Track original state for dirty checking

Event Ordering

  1. Persist all changes first
  2. Commit transaction
  3. Dispatch collected domain events
  4. If dispatch fails, changes are already committed (eventual consistency)

Naming Conventions

Component Pattern Example
State Enum EntityState EntityState
Main Interface UnitOfWorkInterface UnitOfWorkInterface
Implementation Doctrine{Name} DoctrineUnitOfWork
Tracker AggregateTracker AggregateTracker
Transaction TransactionManagerInterface TransactionManagerInterface
Test {ClassName}Test DoctrineUnitOfWorkTest

Quick Template Reference

UnitOfWorkInterface

interface UnitOfWorkInterface
{
    public function begin(): void;
    public function commit(): void;
    public function rollback(): void;
    public function registerNew(object $entity): void;
    public function registerDirty(object $entity): void;
    public function registerDeleted(object $entity): void;
    public function flush(): void;
}

EntityState

enum EntityState: string
{
    case New = 'new';
    case Clean = 'clean';
    case Dirty = 'dirty';
    case Deleted = 'deleted';

    public function canTransitionTo(self $next): bool;
}

Usage Pattern

$unitOfWork->begin();

try {
    $order = $orderRepository->findById($orderId);
    $order->confirm();
    $unitOfWork->registerDirty($order);

    $payment = Payment::create($order->totalAmount());
    $unitOfWork->registerNew($payment);

    $unitOfWork->flush();
    $unitOfWork->commit();
} catch (\Throwable $e) {
    $unitOfWork->rollback();
    throw $e;
}

DI Configuration

# Symfony services.yaml
Application\Shared\UnitOfWork\UnitOfWorkInterface:
    alias: Infrastructure\Persistence\UnitOfWork\DoctrineUnitOfWork

Domain\Shared\UnitOfWork\TransactionManagerInterface:
    alias: Infrastructure\Persistence\UnitOfWork\DoctrineTransactionManager

Database Notes

No dedicated table needed — Unit of Work operates on existing aggregate tables. Requires database that supports transactions (PostgreSQL, MySQL with InnoDB).


References

For complete PHP templates and test examples, see:

  • references/templates.md — All component templates
  • references/examples.md — Order + Payment transaction example and unit tests

Current listing version

0.0.1

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value78/100

Highlights

  • 为 PHP 8.4 提供完整的 Unit of Work 模式基础设施生成,解决多聚合事务一致性问题
  • 涵盖聚合跟踪、刷新/回滚、领域事件收集及单元测试,功能闭环完整
  • 面向 DDD 架构开发者,具有明确的实用价值
Trigger Precision92/100

Highlights

  • 触发描述清晰完整,明确指出为 PHP 8.4 生成 Unit of Work 模式组件
  • 覆盖事务一致性、聚合跟踪、刷新/回滚、领域事件收集等核心能力,边界清晰
Structure and Standards88/100

Areas to improve

  • 文档长度略长 Reported severity: Minor

    文档总行数约 400 行,虽未严重超标,但建议拆分或精简部分章节以提升可读性

    Recommendation: 考虑将 Quick Template Reference 或 DI Configuration 移至独立文件,或精简重复说明

Highlights

  • 结构完整,包含 When to Use、Generation Process、Key Principles 等必要章节
  • 步骤划分清晰(Step 1-3),提供端到端 Usage Pattern 示例
  • frontmatter 完整,命名规范明确
Execution Reliability72/100

Areas to improve

  • 依赖 Doctrine/Symfony 未明确说明 Reported severity: Major

    Skill 中提到了 DoctrineUnitOfWork 和 Symfony DI 配置,但未在前提条件中明确声明依赖,可能导致用户环境不兼容

    Recommendation: 在 When to Use 或前置条件中明确说明需要 Doctrine ORM 和 Symfony 框架支持,或提供替代方案

  • 异常处理细节不足 Reported severity: Minor

    未详细说明 flush 失败时的回滚机制、事件派发失败的处理策略等关键异常场景

    Recommendation: 补充异常处理的最佳实践说明,特别是事务边界内的错误恢复机制

Highlights

  • 提供了清晰的生成流程和文件放置规范,执行路径明确
  • 包含数据库事务支持要求说明(PostgreSQL/MySQL InnoDB)
Evidence Verifiability (mean of the first four)83/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.