Skip to content

Instantly share code, notes, and snippets.

@fjeldstad
Last active September 29, 2015 08:24
Show Gist options
  • Save fjeldstad/48209f57d480917a4ed2 to your computer and use it in GitHub Desktop.
Save fjeldstad/48209f57d480917a4ed2 to your computer and use it in GitHub Desktop.
interface IPipe
{
Type StateType { get; }
Type[] InputMessageTypes { get; }
IPipeOutput Transform(object message, object state);
}
public interface IPipeOutput
{
object[] Messages { get; }
object State { get; }
}
interface IMemory
{
Type StateType { get; }
Type[] InputMessageTypes { get; }
IMemoryAccess Access(object message);
}
interface IMemoryAccess
{
Func<Task<object>> Get { get; }
Func<object, Task> Save { get; }
}
interface IBus
{
Task Publish(object message);
}
async Task Main()
{
// Fake having memory, pipe, message and bus. This will be tied together by the plumber.
var memory = (IMemory)(new object());
var pipe = (IPipe)(new object());
var inputMessage = new object();
var bus = (IBus)(new object());
// Extract state getter and setter from the memory, based on the message being handled.
var memoryAccess = memory.Access(inputMessage);
var state = await memoryAccess.Get();
var output = pipe.Transform(inputMessage, state);
await memoryAccess.Save(output.State); // Could throw concurrency conflict exception. Let the bus retry delivery of message.
foreach (var outputMessage in output.Messages)
{
await bus.Publish(outputMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment