Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

ddd

> 编程语言
Open source

Domain Driven Design PHP helper classes

660 stars0 likes1 views
WebsiteGitHub

About

Domain Driven Design PHP helper classes

carlosbuenosvinos/ddd

This library will help you with typical DDD scenarios, for now:

  • Application Services Interface
  • Transactional Application Services with Doctrine and ADODb
  • Data Transformers Interface
  • No Transformer Data Transformers
  • Domain Event Interface
  • Event Store Interface
  • Event Store Doctrine Implementation
  • Domain Event Publishing Service
  • Messaging Producer Interface
  • Messaging Producer RabbitMQ Implementation

Sample Projects

There are some projects developed using carlosbuenosvinos/ddd library. Check some of them to see how to use it:

  • Last Wishes: Actions to run, such as tweet, send emails, etc. in case anything happen to you.

Application Services

Application Service Interface

Consider an Application Service that registers a new user in your application.

$signInUserService = new SignInUserService(
    $em->getRepository('MyBC\Domain\Model\User\User')
);

$response = $signInUserService->execute(
    new SignInUserRequest(
        '[email protected]',
        'thisisnotasecretpassword'
    )
);

$newUserCreated = $response->getUser();
//...

We need to pass in the constructor all the dependencies. In this case, the User repository. As DDD explains, the Doctrine repository is implementing a generic interface for User repositories.

userRepository = $userRepository;
    }

    /**
     * @param SignInUserRequest $request
     * @return SignInUserResponse
     * @throws UserAlreadyExistsException
     */
    public function execute($request = null)
    {
        $email = $request->email();
        $password = $request->password();

        $user = $this->userRepository->userOfEmail($email);
        if (null !== $user) {
            throw new UserAlreadyExistsException();
        }

        $user = new User(
            $this->userRepository->nextIdentity(),
            $email,
            $password
        );

        $this->userRepository->persist($user);

        return new SignInUserResponse($user);
    }
}

I suggest to make your Application Services implement the following interface following the command pattern.

/**
 * Interface ApplicationService
 * @package Ddd\Application\Service
 */
interface ApplicationService
{
    /**
     * @param $request
     * @return mixed
     */
    public function execute($request = null);
}

Transactions

Application Services should manage transactions when dealing with database persistence strategies. In order to manage it cleanly, I provide an Application Service decorator that wraps an Application Service an executes it inside a transactional boundary.

The decorator is the Ddd\Application\Service\TransactionalApplicationService class. In order to create one, you need the non transactional Application Service and a Transactional Session. We provide different types of Transactional Sessions. See how to do it with Doctrine.

Doctrine Transactional Application Services

For the Doctrine Transactional Session, pass the EntityManager instance.

/** @var EntityManager $em */
$txSignInUserService = new TransactionalApplicationService(
    new SignInUserService(
        $em->getRepository('MyBC\Domain\Model\User\User')
    ),
    new DoctrineSession($em)
);

$response = $txSignInUserService->execute(
    new SignInUserRequest(
        '[email protected]',
        'thisisnotasecretpassword'
    )
);

$newUserCreated = $response->getUser();
//...

As you can see, the use case creation and execution is the same as the non transactional, the only difference is the decoration with the Transactional Application Service.

As a collateral benefit, the Doctrine Session manages internally the flush method, so you don't need to add a flush in your Domain neither your infrastructure.

Asynchronous AMQP listeners

This library is capable to support asynchronous messaging in order to make Bounded Context capable to listen to other Bounded Context's events in an efficient way. The base for this is the both the amqp and the react's event loop. In addition, to support more efficient event loopings, we recommend the installation of one of this extensions

  • libevent
  • libev
  • event

The usage of any of this extensions is handled by ReactPHP's event-loop in a totally transparent way.

Example

Supose we need to listen to the Acme\Billing\DomainModel\Order\OrderWasCreated event triggered via messaging from another bounded context. The following, is an example of a AMQP exchange listener that listents to the Acme\Billing\DomainModel\Order\OrderWasCreated event.

…

And this is a possible command to create AMQP workers

…

AMQP Message producer

The intention of the AMQP message producer is to be composed in some other class. The following is an example of the usage of the AMQP message producer.

…

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言