Skip to content

Instantly share code, notes, and snippets.

@caloggins
Created July 22, 2014 17:42
namespace PolyMap
{
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
public class MappedPublisher : IPublisher
{
private readonly IBus bus;
private readonly IMappingEngine mappingEngine;
private readonly Dictionary<Type,Action<Command>> publishMap = new Dictionary<Type, Action<Command>>();
public MappedPublisher(IBus bus, IMappingEngine mappingEngine)
{
this.bus = bus;
this.mappingEngine = mappingEngine;
publishMap.Add(typeof(Start), Publish<Started>);
publishMap.Add(typeof(Stop), Publish<Stopped>);
}
public void Publish(Command command)
{
var publishAction = GetPublishAction(command.GetType());
publishAction(command);
}
private Action<Command> GetPublishAction(Type commandType)
{
var publishAction = (from a in publishMap
where commandType == a.Key
select a.Value).Single();
return publishAction;
}
private void Publish<TEvent>(Command command) where TEvent : Event
{
var toPublish = mappingEngine.DynamicMap<TEvent>(command);
bus.Publish(toPublish);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment