Clean OpenRasta OperationResults

In my post REST in practice and OpenRasta, I alluded to the ability to deliver specific OperationResults based on the actual result of a specific handler.  I had decided to move the responsiblity for this over to an OperationInterceptor that could contain the same logic for all endpoints.

What quickly became apparent was the fact that this could end up resulting in a huge if statement or ugly and difficult to maintain switch statement , so I came up with the concept, the IOperationResultRule.

public interface IOperationResultRule
{
    bool CanHandle(object entity, int statusCode);
    OperationResult GetResult(object entity);
}

What this means is that within my consuming OperationInterceptor I can supply a list of rules that can be tested, using the CanHandle method, to find the one that can deal with the outcome.

Here’s an example of the consuming code:

foreach (var rule in _rules )
{
     if (!rule.CanHandle(entity, context.Response.StatusCode))
     continue;

     context.OperationResult = rule.GetResult(entity);
     break;
}

Where in this example, entity is the object being passed along the pipeline and StatusCode is the current pipeline context StatusCode, which in this instance I can update elsewhere.

The entity contains the information I need to be able to decide which OperationResult to fire. The first one that “CanHandle” it, handles it.

As a result we get easily maintainable, atomically testable code and no ugly switch statement. New OperationResultRules can be added as required!

Lovely.

About Greg Sochanik

a .net software developer at 7digital, on the silicon roundabout. Living in angel with his lovely wife, little baby boy and mischievous cocker spaniel. Follow me on twitter @gregsochanik
This entry was posted in OpenRasta, Software Development and tagged , . Bookmark the permalink.

One Response to Clean OpenRasta OperationResults

  1. A'braham Barakhyahu says:

    How did you add the hypermedia links? in the response body, or in the response header? How do you do either?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>