xiaohongshu
xiaohongshu
implementing-ddd-architecture
Design and implement DDD patterns (entities, value objects, aggregates, CQRS). Use when creating new domain objects, implementing bounded contexts, designing repository interfaces, or learning proper layer separation. For fixing existing Deptrac violations, use the deptrac-fixer skill instead.
Full skill instructions
Design and implement rich domain models following DDD, hexagonal architecture, and CQRS patterns.
Success Criteria:
make deptrac shows zero violationsRich Domain Models, Not Anemic
Business logic belongs in the Domain layer. Application layer orchestrates, Domain executes.
Domain ─────────────────> (NO dependencies - pure PHP)
│
│
Application ──────────> Domain + Infrastructure
│
│
Infrastructure ───────> Domain + Application
Allowed Dependencies:
| Layer | Can Import |
|---|---|
| Domain | ❌ Nothing (pure PHP, SPL, domain-specific libraries only) |
| Application | ✅ Domain, Infrastructure, Symfony, API Platform |
| Infrastructure | ✅ Domain, Application, Symfony, Doctrine ORM |
Template examples may show Doctrine ODM/MongoDB constructs. In this service, use Doctrine ORM with MySQL (
EntityManagerInterface,.orm.xmlmappings).
See: DIRECTORY-STRUCTURE.md for complete file placement guide.
❌ FORBIDDEN in Domain:
use Symfony\...)✅ ALLOWED in Domain:
❌ BAD (Anemic):
class Customer {
public function setName(string $name): void {
$this->name = $name; // No validation!
}
}
✅ GOOD (Rich):
class Customer {
public function changeName(CustomerName $name): void {
// Business rules enforced
$this->record(new CustomerNameChanged($this->id, $name));
$this->name = $name;
}
}
❌ BAD: Validation in Domain with Symfony
use Symfony\Component\Validator\Constraints as Assert;
class Customer {
#[Assert\NotBlank] // ❌ Framework in Domain!
private string $name;
}
✅ GOOD: Validation in YAML config (Preferred)
# config/validator/Customer.yaml
App\Application\DTO\CustomerCreate:
properties:
name:
- NotBlank: ~
- Length:
min: 2
max: 100
Framework validators should always be used when possible. They provide:
Value Objects should only be used when:
See: REFERENCE.md for complete validation patterns.
// src/Core/{Context}/Application/Command/{Action}{Entity}Command.php
final readonly class CreateCustomerCommand implements CommandInterface
{
public function __construct(
public string $id,
public string $name,
public string $email
) {}
}
// src/Core/{Context}/Application/CommandHandler/{Action}{Entity}CommandHandler.php
final readonly class CreateCustomerCommandHandler implements CommandHandlerInterface
{
public function __invoke(CreateCustomerCommand $command): Customer
{
// Minimal orchestration only
$customer = Customer::create(
Ulid::fromString($command->id),
new CustomerName($command->name),
new Email($command->email)
);
$this->repository->save($customer);
$this->eventBus->publish(...$customer->pullDomainEvents());
return $customer;
}
}
See: REFERENCE.md for complete CQRS patterns.
// src/Core/{Context}/Domain/Repository/{Entity}RepositoryInterface.php
interface CustomerRepositoryInterface
{
public function save(Customer $customer): void;
public function findById(string $id): ?Customer;
}
// src/Core/{Context}/Infrastructure/Repository/{Entity}Repository.php
final class CustomerRepository implements CustomerRepositoryInterface
{
public function __construct(
private readonly DocumentManager $documentManager
) {}
public function save(Customer $customer): void
{
$this->documentManager->persist($customer);
$this->documentManager->flush();
}
}
Register in config/services.yaml:
App\Core\Customer\Domain\Repository\CustomerRepositoryInterface:
alias: App\Core\Customer\Infrastructure\Repository\CustomerRepository
class Customer extends AggregateRoot // Provides event recording
{
public function changeName(CustomerName $name): void
{
$this->name = $name;
$this->record(new CustomerNameChanged($this->id, $name));
}
}
// src/Core/{Context}/Application/EventSubscriber/{Event}Subscriber.php
final readonly class CustomerNameChangedSubscriber implements DomainEventSubscriberInterface
{
public function __invoke(CustomerNameChanged $event): void
{
// React to event (e.g., send notification)
}
}
See: REFERENCE.md for complete event-driven patterns.
Domain/Entity/Domain/ValueObject/Domain/Repository/Infrastructure/Repository/Application/Command/Application/CommandHandler/make deptrac shows zero violationsSee: examples/ for complete working examples.
If make deptrac shows violations:
Use: deptrac-fixer skill for step-by-step fix patterns.
deptrac.yaml to allow violationsmake deptrac after changessrc/Core/{Context}/
├── Domain/
│ ├── Entity/
│ │ └── {Entity}.php # Pure PHP, no attributes
│ ├── ValueObject/
│ │ └── {ValueObject}.php # Validation logic here
│ ├── Repository/
│ │ └── {Entity}RepositoryInterface.php
│ ├── Event/
│ │ └── {Event}.php
│ └── Exception/
│ └── {Exception}.php
├── Application/
│ ├── Command/
│ │ └── {Action}{Entity}Command.php
│ ├── CommandHandler/
│ │ └── {Action}{Entity}CommandHandler.php
│ └── EventSubscriber/
│ └── {Event}Subscriber.php
└── Infrastructure/
└── Repository/
└── {Entity}Repository.php
✅ No violations found
After implementing DDD patterns:
CommandInterfaceCommandHandlerInterfaceDomainEventSubscriberInterfacemake deptrac shows zero violationsmake ci passesFor detailed patterns, workflows, and examples:
// ❌ BAD: Logic in handler
class CreateCustomerHandler {
public function __invoke($command) {
if (strlen($command->name) < 2) { // ❌ Validation in handler!
throw new Exception();
}
// ...
}
}
// ❌ BAD: Symfony in Domain
use Symfony\Component\Validator\Constraints as Assert;
class Customer {
#[Assert\NotBlank] // ❌ Framework coupling!
private string $name;
}
// ❌ BAD: Just getters/setters
class Customer {
public function setName(string $name): void {
$this->name = $name; // No business rules!
}
}
This project follows CodelyTV's hexagonal architecture patterns:
See: DIRECTORY-STRUCTURE.md for complete hierarchy.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience