【问题标题】:CQRS in data-centric processes以数据为中心的流程中的 CQRS
【发布时间】:2014-09-19 06:25:46
【问题描述】:

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

在我们的集成过程中,我们有大量结构化数据,而不是一组命令/事件,我想知道如何实际处理这些数据。

// 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
    }
}

我的问题是我的建议是否适合给定的情况,或者可能有一些更好的方法来做我需要的事情。提前谢谢你。

【问题讨论】:

  • 看起来不错,尽管这取决于您的需求。您在针对您的应用程序服务进行这 2 次调用时是否遇到任何问题?

标签: domain-driven-design integration cqrs


【解决方案1】:

同意,服务可能有不同的接口。如果您创建一个 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);
}

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2020-01-24
    • 2018-08-17
    • 2010-11-23
    相关资源
    最近更新 更多