CQRS以数据为中心的流程

时间:2014-07-28 05:33:12

标签: domain-driven-design integration cqrs

我在数据中心流程中遇到了与CQRS相关的问题。让我更好地解释一下。 考虑我们有一个SOAP / JSON /任何服务,它在集成过程中将一些数据传输到我们的系统。据说,在CQRS中,每个状态变化必须通过命令(或使用事件源)的事件来实现。

当谈到我们的整合过程时,我们得到了大量的结构化数据而不是一组命令/事件,我想知道如何实际处理这些数据。

// Some Façade service
class SomeService
{
    $_someService;

    public function __construct(SomeService $someService)
    {
        $this->_someService = $someService;
    }

    // Magic function to make it all good and
    public function process($dto)
    {
       // if I get it correctly here I need somehow 
       // convert incoming dto (xml/json/array/etc)
       // to a set of commands, i. e
       $this->someService->doSomeStuff($dto->someStuffData);        
         // SomeStuffChangedEvent raised here

       $this->someService->doSomeMoreStuff($dtom->someMoreStuffData);
         // SomeMoreStuffChangedEvent raised here
    }
}

我的问题是我的建议是否适用于特定情况,或者可能有更好的方法来完成我需要的工作。提前谢谢。

1 个答案:

答案 0 :(得分:0)

同意,服务可能有不同的界面。如果您创建rest-api来更新员工,则可能需要提供UpdateEmployeeMessage,其中包含可以更改的所有内容。在CRUD类型的服务中,此消息可能会镜像数据库。

在服务内部,您可以将消息拆分为命令:

public void Update(UpdateEmployeeMessage message)
{
    bus.Send(new UpdateName
    {
        EmployeeId = message.EmployeeId,
        First = message.FirstName,
        Last = message.LastName,
    });

    bus.Send(new UpdateAddress
    {
        EmployeeId = message.EmployeeId,
        Street = message.Street,
        ZipCode = message.ZipCode,
        City = message.City
    });

    bus.Send(new UpdateContactInfo
    {
        EmployeeId = message.EmployeeId,
        Phone = message.Phone,
        Email = message.Email
    }); 
}

或者您可以直接调用聚合:

public void Update(UpdateEmployeeMessage message)
{
    var employee = repository.Get<Employee>(message.EmployeeId);

    employee.UpdateName(message.FirstName, message.LastName);
    employee.UpdateAddress(message.Street, message.ZipCode, message.City);
    employee.UpdatePhone(message.Phone);
    employee.UpdateEmail(message.Email);

    repository.Save(employee);
}