Create unit of work

From dykyi-roman/awesome-claude-code · View interactive version

Create unit of work is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. 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.

How to use this skill

Skill file

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

UnitOfWork

AggregateTracker

TransactionManagerInterface

DomainEventCollector


Generation Process

Step 1: Analyze Request

Determine:

Step 2: Generate Core Components

Create in this order:

  1. Domain Layer (src/Domain/Shared/UnitOfWork/)
  1. Application Layer (src/Application/Shared/UnitOfWork/)
  1. Infrastructure Layer (src/Infrastructure/Persistence/UnitOfWork/)
  1. Tests

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:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.