1

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:

  1. 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).
  2. We convert the input model to a command for a bus.
  3. We send the prepared change command through the bus to be processed.
  4. 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.

8
  • 2
    you could start by not returning 200-OK, but instead return something that is more akin to what you are actually doing like 202-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)
    – Sam Holder
    Sep 29, 2015 at 15:14
  • 1
    This is a good suggestion but it sounds like a customer knows about an internal implementation is job-like and they need to come back later to check a result. It works for heavy jobs but for a simple record update would be overkill.
    – slo2ols
    Sep 29, 2015 at 15:40
  • if you don't want to expose this to the customer then you'll have to do something like creating a subscription on the bus to the event which indicates success or failure and wait until either one of those events is received, then you can return 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 a 303-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.
    – Sam Holder
    Sep 29, 2015 at 15:53
  • This subscription will be trigger in async manner and everything will work exactly how I described in the example for a poor designed system. Using of 303 status code (as well as 302) leads us to racing between the change application and querying the result.
    – slo2ols
    Sep 29, 2015 at 16:26
  • 2
    This is what I'm using.
    – MikeSW
    Oct 1, 2015 at 13:42

1 Answer 1

3

As I suggested in the comments you could wait for the event which signals completion and only return to the user when this is received. Some pseudo code:

public IHttpActionResult ChangeVendor(ChangeVendorModel changeModel)
{        
    var changeCommand = changeModel.MapTo<ChangeVendorCommand>();   
    bus.Send(changeCommand); // start the change processing
    var replyReceived=false;
    bool success = false;
    while(!replyreceived)
    {
        Task vendorChanged = Task.Factory.StartNew(()=> 
            {
                 var reply=bus.Receive<VendorChanged>());
                 if(reply.CorrelationToken==changeCommand.CorrelationToken)
                 {
                      replyReceived=true;
                      success=true;
                 }
            },SomeTimeout);
        Task vendorChangedFailed = Task.Factory.StartNew(()=> 
            {
                 var reply=bus.Receive<VendorChangeFailed>());
                 if(reply.CorrelationToken==changeCommand.CorrelationToken)
                 {
                      replyReceived=true;
                      success=false;
                 }
            },SomeTimeout());
        Task.WaitAny(new Task[]{vendorChanged,vendorChangeFailed});

    }
    if(success)
    {
        return Ok(); 
    }
    else
    { 
        return ChangeVendorFailed();
    }
}

obviously the receive needs to be on its own subscription, to ensure it doesn't take replies for other instances, and you may be able to create the subscription to receive only messages with the correct correlation token or other identifying property, but this gives you sopme idea of one way to skin this cat and make your tasky async workflow look syncronous to the user

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.