Adapters
An adapter is a middleware component that can be executed at any point in the pipeline and it has a single conceptual purpose: to adapt data. Adapters are transformation steps in the pipeline. They should not contain business logic.
graph LR;
in[/Data In/] --> Adapter --> out[/Data Out/]
An adapter is expected to forward data to the next component in the pipeline, although that might not always be the case. If an adapter doesn't forward any data, the pipeline is interrupted and no other components will be executed.
Implementing an Adapter
Unless there are very specific needs, you should inherit your adapter from the ChannelAdapter<T> abstract class instead of implementing the IChannelAdapter interface directly.
You also need to indicate whether the adapter is meant for the input and/or the output pipeline. We do that by adding the interfaces IInputChannelAdapter and/or IOutputChannelAdapter respectively.
Here's an example of how to implement an adapter that adapts from a IReadableByteBuffer. This adapter can only be added to the input pipeline.
public class MyChannelAdapter : ChannelAdapter<IReadableByteBuffer>, IInputChannelAdapter
{
public override Task ExecuteAsync( IAdapterContext context, IReadableByteBuffer data, CancellationToken cancellationToken )
{
// adapt/transform data
var adaptedData = ...
// forward adapted data
context.Forward( adaptedData );
}
}
Ready-made Adapters
In addition to the abstract ChannelAdapter<T> adapter, you have a few ready-made adapters that you can use.
| Adapter | Target | Description |
|---|---|---|
| AnonymousChannelAdapter | Input/Output | A quick way to implement an anonymous adapter |
| BufferLengthAdapter | Input | Ensures the input buffer doesn't exceed in length |