jason22_thumb4

When dealing with a third party toolkit/framework, such as Jason, it is really important to be able to instruct the toolkit to behave in some sort of default way if something is not provided/expected.

We spoke about validation but what if we would like to plug a default DataAnnotation validator that intercept all the incoming commands and handles all the applied attributes? Such as:

public sealed class ObjectDataAnnotationValidator : AbstractDataAnnotationValidator<Object>
{
}

the first thing to notice is that Jason supports inheritance so we can inject an AbstractDataAnnotationValidator<Object> to intercept _all_ the commands regardless of their type. We can instruct Jason to treat the above validator as a fallback validator if no validator, for the incoming command type, can be found in the following way:

var jasonConfig = new DefaultJasonServerConfiguration
(
  pathToScanForAssemblies: Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "bin" ),
  assemblySelectPattern: assemblySelectPattern
)
{
  Container = new WindsorJasonContainerProxy( container ),
  TypeFilter = t => !t.Is<JasonToBusRepublishHandler>()
};

jasonConfig.AddEndpoint( new Jason.WebAPI.JasonWebAPIEndpoint()
{
    TypeNameHandling = TypeNameHandling.Objects,
    DefaultSuccessfulHttpResponseCode = System.Net.HttpStatusCode.Accepted
} )
.UsingAsFallbackCommandHandler<JasonToBusRepublishHandler>()
.UsingAsFallbackCommandValidator<ObjectDataAnnotationValidator>()
.Initialize();

The above snippet comes from a web application that is a mere frontend that receives commands from SPA client (Single Page Application) so the applications performs a basic validation and republish commands on the bus (using NServiceBus in this case), so we configure Jason to do 2 different things:

  1. if a command handler for the incoming command cannot be found use the JasonToBusRepublishHandler;
  2. Since we are using a bus to dispatch commands we are introducing delays, due the async nature of a bus, in the command handling pipeline thus we instruct Jason for WebAPI to return as success HTTP status code “Accepted” instead of the default HTTP-200;
  3. The last thing to notice is that in the configuration we are telling Jason to ignore (using the TypeFilter delegate) the JasonToBusRepublishHandler that should not be registered as a traditional handler in the Jason configuration;

.m