Let's assume I have the next pseudo code to implement Command based change in terms of CQRS (actually, Event Sourcing is questionable as well) in my WebApi project:
public IHttpActionResult ChangeVendor(ChangeVendorModel changeModel)
{
/* 1 */ // user input validation
/* 2 */ var changeCommand = changeModel.MapTo<ChangeVendorCommand>();
/* 3 */ bus.Send(changeCommand); // start the change processing
/* 4 */ return Ok();
}
The explanation:
- We perform a basic user input validation (as string length or only positive numbers) but not a business validation (as this Vendor is in the black list).
- We convert the input model to a command for a bus.
- We send the prepared change command through the bus to be processed.
- By this we mean the change was applied and a domain model is available for the further manipulations.
The questions:
a. The bus processing is asynchronous. How can I be sure (after step 4) that my changes were applied and the app is ready to render success view displaying a changed record from a database designed for querying purposes?
b. Let's say the record version conflict happened (data violation) or a model was not passed through the business rules (domain violation). How can I instantly notify a user about this from the bus? In a bad designed system, a user could see a successful result because we successfully scheduled the change on the bus and later on they could see the notification with an error when the attempt to apply the actual change was made.
200-OK
, but instead return something that is more akin to what you are actually doing like202-ACCEPTED
. Then the consumer will know that they need to check the result of the call at some point in the future (you might send a Location header or similar to let the client know where to look for the result)200-OK
or an appropriate error code instead (or timeout). You can't pretend your internal implementation isn't job-like unless you do something like this. You might also consider returning a303-SEE OTHER
to allow the client to get the response, and hope the most of the time the processing will be completed by the time the client gets there.