simplycodedsoftware / integration-messaging-cqrs
Requires
- php: >=7.1.0
- doctrine/orm: ^2.5
- simplycodedsoftware/integration-messaging: ^0.4.0
Requires (Dev)
- behat/behat: ^3.3.1
- phan/phan: ^0.10.0
- phpunit/phpunit: ^6.2.3
README
模块提供 CQRS 功能。适用于服务和聚合。
如果您不熟悉这里使用的命名,请阅读集成消息文档。
使用方法
服务命令处理程序
/**
* @MessageEndpointAnnotation(referenceName="serviceCommandHandlerExample")
*/
class CommandHandlerServiceExample
{
/**
* @CommandHandlerAnnotation()
*/
public function doAction(SomeCommand $command) : void
{
}
}
服务查询处理程序
/**
* @MessageEndpointAnnotation()
*/
class QueryHandlerServiceExample
{
/**
* @QueryHandlerAnnotation()
*/
public function searchFor(SomeQuery $query) : SomeResult
{
return new SomeResult();
}
}
聚合命令处理程序
/**
* @AggregateAnnotation()
*/
class Order
{
/**
* @param DoStuffCommand $command
* @CommandHandlerAnnotation()
*/
public static function register(RegisterNewOrder $command) : void
{
// do something
}
public functon cancel(CancelOrder $command) : void
{
// do something
}
}
class CancelOrder
{
/**
* @var string
* @AggregateIdAnnotation()
*/
private $orderId;
/**
* @var int
* @AggregateExpectedVersionAnnotation()
*/
private $version;
}
聚合查询处理程序
/**
* @AggregateAnnotation()
*/
class AggregateQueryHandlerExample
{
/**
* @QueryHandlerAnnotation()
*/
public function doStuff(SomeQuery $query) : SomeResult
{
return new SomeResult();
}
}
如何发送消息
注入到您的控制器/服务命令网关或查询网关
SimplyCodedSoftware\IntegrationMessaging\Cqrs\CommandGateway
或 SimplyCodedSoftware\IntegrationMessaging\Cqrs\QueryGateway
将自动在您的容器中注册。
它们可以通过类名访问,所以如果您的容器可以执行自动装配,那么您只需在方法声明中简单地进行类型提示,即可获取它们。
手动发送
将消息作为查询或命令发送到 integration_messaging.cqrs.execute_message
消息通道
您自己的流程
如果您需要在执行 command
/ query
之前执行某些操作,例如反序列化 json,那么您可以受益于 Message Flow
扩展。
这允许将特定名称与消息类名称相关联。您可以定义默认流程,所有消息都将通过该流程,或为特定用例定义自定义流程。
此外,流程还将处理消息名称到类名称的映射,因此您知道要反序列化什么。
要为消息开始流程,您可以使用 SimplyCodedSoftware\IntegrationMessaging\Cqrs\MessageFlowGateway
,您需要传递的是与 SimplyCodedSoftware\IntegrationMessaging\Cqrs\Annotation\MessageFlowAnnotation
中定义的消息名称相同。
然后您需要定义默认通道,消息将发送到该通道 integration_messaging.cqrs.start_default_flow
,如果您为消息定义了自定义流程,那么您还需要在 message name
之后 创建通道
。
在头部中,您将可以访问在 integration_messaging.cqrs.message_class
下定义的类映射。