If you have previously used Web API or Windows Communication Framework (WCF) you will find yourself in the habit of writing service methods specialized for only one scenario.
A typical interface to search through Task
instances would be something, like the following:
There is basically a separate and specialized method for each search option.
In contrast, according to the message pattern, this would be implemented as follows:
Note
Additionally, to the basic definition of the message, ServiceStack.IReturn<T>
is already used here. There is no need whatsoever to implement this interface, but doing so for example gives you the possibility to deviate from the naming convention of ResponseDTO class names for the metadata page, and defines the return type on service clients Send
methods.
This combines the various search options into one message, which makes the following benefits obvious:
- Less distribution of logic
- Less maintenance due to less code duplication in the long run
- Easily add more functionality by introducing new properties in the message without adapting to existing usages that gives you a straightforward approach to various versions
- Less friction with caching, as the instances can be used to generate a cache key
- Easy to serialize and log
- When immutable, it's perfect for concurrency and multithreaded scenarios
To show these benefits in action, let's contrast the implementations, which are by no means optimized or perfectly well implemented:
This basic Service
class holds an array of Task
objects that are used in every method for the specific query. Then the matching excerpt of the array is returned.
In a message-based service it would look like:
The implementation of the actual endpoint is straightforward, just apply each filter prior to checking against null and return a matching excerpt.
Note
The added ServiceStack.IAny<T>
naturally forces an implementation of the request in the TaskService
class. You can still add your operation to the service manually, but I strongly advise you to follow the New API outline available at https://github.com/ServiceStack/ServiceStack/wiki/New-API.
This implementation can be easily connected to the following web page. It once again shows the power of the Code-First approach as it binds to the following interface with ease: