patterncsharpMinor
Event Subscriber with Parallelism
Viewed 0 times
withsubscriberparallelismevent
Problem
I am trying to exploit asynchronism for parallelism. This is my first attempt at a parallel event subscriber. In your expert opinions, is this a valid approach?
```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EventStore.ClientAPI;
namespace Sandbox
{
public class SomeEventSubscriber
{
private Position? _latestPosition;
private readonly Dictionary> _eventHandlerMapping;
private IEventStoreConnection _connection;
public Dictionary> EventHandlerMapping
{
get { return _eventHandlerMapping; }
}
public SomeEventSubscriber()
{
_eventHandlerMapping = CreateEventHandlerMapping();
_latestPosition = Position.Start;
}
public void Start()
{
ConnectToEventstore();
}
private void ConnectToEventstore()
{
_connection = EventStoreConnectionWrapper.Connect();
_connection.Connected +=
(sender, args) => _connection.SubscribeToAllFrom(_latestPosition, false, EventOccured, LiveProcessingStarted, HandleSubscriptionDropped);
}
private Dictionary> CreateEventHandlerMapping()
{
return new Dictionary>
{
{typeof (FakeEvent1), o => Handle(o as FakeEvent1)},
{typeof (FakeEvent2), o => Handle(o as FakeEvent2)},
};
}
private void Handle(FakeEvent1 eventToHandle)
{
SomethingLongRunning(eventToHandle);
}
private void Handle(FakeEvent2 eventToHandle)
{
SomethingLongRunning(eventToHandle);
}
private void SomethingLongRunning(BaseFakeEvent eventToHandle)
{
Console.WriteLine("Start Handling: " + eventToHandle.GetType());
for (int i = 0; i _eventHandlerMappingeventType);
Console.WriteLine("The task is running asynchro
```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EventStore.ClientAPI;
namespace Sandbox
{
public class SomeEventSubscriber
{
private Position? _latestPosition;
private readonly Dictionary> _eventHandlerMapping;
private IEventStoreConnection _connection;
public Dictionary> EventHandlerMapping
{
get { return _eventHandlerMapping; }
}
public SomeEventSubscriber()
{
_eventHandlerMapping = CreateEventHandlerMapping();
_latestPosition = Position.Start;
}
public void Start()
{
ConnectToEventstore();
}
private void ConnectToEventstore()
{
_connection = EventStoreConnectionWrapper.Connect();
_connection.Connected +=
(sender, args) => _connection.SubscribeToAllFrom(_latestPosition, false, EventOccured, LiveProcessingStarted, HandleSubscriptionDropped);
}
private Dictionary> CreateEventHandlerMapping()
{
return new Dictionary>
{
{typeof (FakeEvent1), o => Handle(o as FakeEvent1)},
{typeof (FakeEvent2), o => Handle(o as FakeEvent2)},
};
}
private void Handle(FakeEvent1 eventToHandle)
{
SomethingLongRunning(eventToHandle);
}
private void Handle(FakeEvent2 eventToHandle)
{
SomethingLongRunning(eventToHandle);
}
private void SomethingLongRunning(BaseFakeEvent eventToHandle)
{
Console.WriteLine("Start Handling: " + eventToHandle.GetType());
for (int i = 0; i _eventHandlerMappingeventType);
Console.WriteLine("The task is running asynchro
Solution
var task = Task.Factory.StartNew(() => _eventHandlerMapping[eventType](@event));
Console.WriteLine("The task is running asynchronously...");
task.Wait();This code doesn't make any sense. You're running the event handler on another thread, but then you're immediately waiting for it to finish. The end result is your code will behave exactly as if you called the handler directly, only with more code and some added overhead.
Code Snippets
var task = Task.Factory.StartNew(() => _eventHandlerMapping[eventType](@event));
Console.WriteLine("The task is running asynchronously...");
task.Wait();Context
StackExchange Code Review Q#74090, answer score: 2
Revisions (0)
No revisions yet.