Useful bits for foundational groundwork in C# applications.
- MessageBus for async dispatch of messages between components in the same
process
class Receiver : IHandle<string>{ public void Handle(string message) => Console.WriteLine(message); } ... var messageBus = MessageBus.Create() messageBus.Subscribe( new Receiver(), SubscriptionLifecycle.ExplicitUnsubscribe); ... await messageBus.Publish("Hello, World!");
- Stateful agent to handle shared mutable state without having to use explicit
lock statements or other semaphores
enum CounterCommand{ Increment, Decrement, Current, } var counter = Agent.Start<CounterCommand,int,int>( 0, (current,command) => command switch{ CounterCommand.Increment => (current+1, current+1), CounterCommand.Decrement => (current-1, current-1), CounterCommand.Current => (current,current), _=>throw new ArgumentException("unknown command") } ) ... //thread/task 1 await counter.Tell(CounterCommand.Increment); //thread/task 2 await counter.Tell(CounterCommand.Decrement); ... //output: "0" regardless of context switches, as long as pervious two calls are started before Console.WriteLine(await counter.Tell(CounterCommand.Current));
- Stateless agent to ensure generalized sequential execution of actions triggered from different background tasks without explicit synchronization
- Andreas Pfohl
- Dirk Peters