Skip to content

Instantly share code, notes, and snippets.

@perokvist
perokvist / Middleware
Created April 28, 2018 16:16
Robots Middleware
public class RobotsMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public RobotsMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
_next = next;
_logger = loggerFactory.CreateLogger<RobotsMiddleware>();
}
@perokvist
perokvist / ApplicationService
Last active June 13, 2017 11:56
ApplicationService util
await ApplicationService.ExecuteAsync( // (await response options 4.2 vs 4.3
eventStore,
command.AggregateId,
command.CorrelationId,
events => events.Rehydrate<AggregateState>(), //aggregate, fold, match
state => Aggregate.Handle(command, state),
log.AppendAsync,
locks,
timeoutHandler,
waitForLockRelease: false // 4.2 vs 4.3
@perokvist
perokvist / EventProcessor
Last active June 14, 2017 08:56
C# EventProcessor
public class EventProcessor
{
private readonly IDictionary<IAggregateId, (Guid, SemaphoreSlim)> _locks;
private readonly Action<string> _logger;
private readonly List<(Type, Func<IEvent, Task>)> _h = new List<(Type, Func<IEvent, Task>)>();
public EventProcessor(IDictionary<IAggregateId, (Guid, SemaphoreSlim)> locks) : this(locks, s => { })
{}
public EventProcessor(IDictionary<IAggregateId, (Guid, SemaphoreSlim)> locks, Action<string> logger)
@perokvist
perokvist / Dispatcher
Created June 9, 2017 13:33
C# Command dispatcher
public class Dispatcher<TMessage, TResult>
{
private readonly IDictionary<Type, Func<TMessage, TResult>> _dictionary = new ConcurrentDictionary<Type, Func<TMessage, TResult>>();
public void Register<T>(Func<T, TResult> func) where T : TMessage
=> _dictionary.Add(typeof(T), x => func((T)x));
public TResult Dispatch(TMessage m)
{
Func<TMessage, TResult> handler;
@perokvist
perokvist / module_example_early_draft.cs
Last active August 29, 2015 14:18
Commit log examples
public class Module
{
public static Func<ICommand, Task> Run(CancellationToken token, Subject<IEvent> eventSubject, Action<string> logger)
{
const string streamName = "the-stream";
var commitLog = new MessageVaultClientAdapter(new MemoryClient(), logger);
var dispatcher = new Dispatcher<ICommand, Task>();
var eventHandlers = new Dispatcher<IEvent, Task>();
var checkpointWriter = new CheckpointWriterAdapter(new MemoryCheckpointReaderWriter());
var repository = new InMemoryStateRepository<State>();
@perokvist
perokvist / BroadcastToX
Last active December 17, 2015 10:49
Lab 2 - Instructions
public class BroadcastToXXX : IBroadcast
{
private readonly IConnectionManager _connectionManager;
public BroadcastToCorrelating(IConnectionManager connectionManager)
{
_connectionManager = connectionManager;
}
public async Task WhenAsync(IEvent @event)
@perokvist
perokvist / Broadcaster
Last active December 17, 2015 10:49
Lab2 - Cheat sheet
public class BroadcastToCorrelating : IBroadcast
{
private readonly IConnectionManager _connectionManager;
public BroadcastToCorrelating(IConnectionManager connectionManager)
{
_connectionManager = connectionManager;
}
public async Task WhenAsync(IEvent @event)
@perokvist
perokvist / IServiceBus
Last active December 13, 2015 18:08
Simple.ServiceBus 0.1
public interface IServiceBus
{
Task PublishAsync<T>(T message);
IDisposable Subscribe<T>(IObserver<T> handler,SubscriptionConfiguration configuration=null);
}
@perokvist
perokvist / ViewModel_to_observable.js
Created October 11, 2012 07:35
Two-way WinJS binding
var viewModel = WinJS.Class.define(function() {
// this is the constructor function
var self = this;
}, {
// object literal for methods and attributes on the "class"
});
var observableViewModel = WinJS.Class.mix(viewModel, WinJS.Binding.mixin, WinJS.Binding.expandProperties(viewModel));
autoLoadTests: function (e) {
var re = new RegExp("([^/]+\.html$)");
var testUrl = e.detail.location.replace(re, QUnit.config.unitTestFileName);