質問

I have a situation in a CQRS project where I have to log a user's request for information (query) then optionally start a workflow based on the response from the data store. The user is making a request for information which requires immediate feedback. At the same time, the system optionally starts a workflow to analyse the request. How do I implement this in CQRS since the request is neither a 'pure' query nor a 'pure' command?

Edit: To add some more context to this: The application is like a search application, where the user types in a query and the application returns with a result. But the application also logs the query and could start a workflow depending on the response from the server. The application also "remembers" the user's last few queries and uses it to give context to the new query.

Additionally, the query response may not be synchronous. A background worker may be responsible for delivering the result to the client.

役に立ちましたか?

解決

Though you've given us little to work with I think this question has a simple answer:

I disagree with you that the request is neither a 'pure' query nor a 'pure' command. The request is a pure query, because the request is not a request for an analysis, but a request for information. The analysis that optionally gets triggered by the request is a command, but a command in the context of the query event. The system, or more specifically the event handler, is therefore the actor in the context of the command, not the user, which is the actor in the context of the query.

No query is ever side-effect free. It is the intention what makes it a query.

他のヒント

Such request is a command.

In simple OOP, I've often modeled such kind of message as a void method with an out params.

For example, in a financial model, I had an advisory contract (entity and aggregate root) enforcing rules to build a financial recommendation (entity, immutable). The pubblication of the definitive recommentation was modeled with a command like this:

public interface IAdvisoryContract
{
    ContractNumber Number { get; }

    // lot of well documented commands and queries here...

    /// 90 lines of documentation here...
    void PublishRecommendation(
               IUser advisor, IAssetClassBreakdownAnalysis assetClassAnalysis,
               IVolatilityAnalysis tevAnalysis, Time time,
               out IRecommendation newRecommendation);

    event EventHandler<EventArgs<RecommendationNumber>> RecommendationPublished;
}

In CQRS it depends on your infrastructure: for example, in a similar situation over HTTP, I used a POST returning the relevant info to the client.

What about after the execution of the query send a message for notification? I probably will use a decorator like:

public QueryRs query(QueryRq rq) {
     final QueryRs rs = target.query(rq);
     notifier.notifyQueryDone(rs);
}

And make the workflow subscribe and consume the message. I'm not sure is this query considered changing the state still in this solution?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top