PHP Classes

File: docs/guides/aem-and-hexagonal-architecture.md

Recommend this page to a friend!
  Packages of ?????? ???????   Adaptive Entity Manager   docs/guides/aem-and-hexagonal-architecture.md   Download  
File: docs/guides/aem-and-hexagonal-architecture.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Adaptive Entity Manager
Entity manager to work with multiple data sources
Author: By
Last change:
Date: 3 months ago
Size: 3,228 bytes
 

 

Contents

Class file image Download

AEM and hexagonal architecture

How to place Adaptive Entity Manager in a ports and adapters (hexagonal) layout so persistence does not leak into the domain.

Idea in one sentence

Domain and application define what to load and persist; AEM lives in infrastructure, behind repository adapters that implement your ports.

Layering

flowchart TB
    subgraph domain [Domain / application]
        AR["Aggregate / entity model"]
        Port["Repository port<br/>(interface)"]
        AR --> Port
    end

    subgraph infra [Infrastructure]
        Adapter["AemXxxRepository<br/>implements port"]
        EM["AdaptiveEntityManager"]
        DA["EntityDataAdapter"]
        Adapter --> EM
        EM --> DA
    end

    subgraph external [External systems]
        DB["Legacy DB"]
        API["HTTP API"]
        FS["Files"]
    end

    Port -.->|implements| Adapter
    DA --> DB
    DA --> API
    DA --> FS

Responsibilities

| Layer | Owns | Does not own | |-------|------|----------------| | Domain | Invariants, value objects, domain services | SQL, HTTP URLs, table names | | Application | Use cases, orchestration | Adapter choice, metadata YAML | | Infrastructure (AEM) | Metadata, hydration, UnitOfWork, adapters | Business rules |

Repository port example

// Domain or application (port)
interface OrderRepository
{
    public function get(OrderId $id): Order;

    public function save(Order $order): void;
}

// Infrastructure (adapter)
final class AemOrderRepository implements OrderRepository
{
    public function __construct(
        private readonly AdaptiveEntityManager $em,
    ) {}

    public function get(OrderId $id): Order
    {
        return $this->em->find(Order::class, $id->value())
            ?? throw new OrderNotFound($id);
    }

    public function save(Order $order): void
    {
        $this->em->persist($order);
        $this->em->flush();
    }
}

Wire the port in your DI container (Symfony, manual, etc.). The bundle is optional; the pattern is the same with a manually constructed AdaptiveEntityManager.

One adapter per data source

Hexagonal style maps cleanly to AEM:

  • Port ? `CustomerRepository`
  • Adapter class ? `EntityDataAdapter` implementation (legacy SQL, REST, ?)
  • Metadata ? maps storage shape to entity properties for that source

Different bounded contexts can use different adapters while sharing the same AEM patterns.

Lazy loading and boundaries

AEM supports lazy associations via proxies. Inside aggregate boundaries, prefer explicit loading in the adapter or application service so invariants stay obvious. See AEM and DDD for aggregate-oriented notes.

Related guides

Stable target

The recommended finish line in MIGRATION_ROADMAP.md is stable adaptive persistence: hexagonal boundaries + AEM + adapters?not a mandatory move to Doctrine ORM.