Skip to content

Instantly share code, notes, and snippets.

@copypastedeveloper
Created June 10, 2014 03:57
Show Gist options
  • Save copypastedeveloper/5e721a8ab9d576e9099f to your computer and use it in GitHub Desktop.
Save copypastedeveloper/5e721a8ab9d576e9099f to your computer and use it in GitHub Desktop.
Automapper based Event Upconversion
public class EventUpconverter<TSource, TDestination> : IUpconvertEvents<TSource, TDestination>
where TSource : class,IEvent
where TDestination : class,IEvent
{
readonly IMappingEngine _mapper;
public EventUpconverter(IMappingEngine mapper)
{
_mapper = mapper;
}
public TDestination Convert(TSource sourceEvent)
{
return _mapper.Map<TDestination>(sourceEvent);
}
}
public static class UpConversionFinder
{
public static List<Type> GetUpconverterTypes()
{
var eventVersionAttribute = typeof(VersionedEventAttribute);
var eventBase = typeof(IEvent);
var upconverterType = typeof (EventUpconverter<,>);
return Mapper.GetAllTypeMaps()
.Where(x => eventBase.IsAssignableFrom(x.SourceType) &&
eventBase.IsAssignableFrom(x.DestinationType) &&
x.SourceType.GetCustomAttributes(eventVersionAttribute, true).Any() &&
x.DestinationType.GetCustomAttributes(eventVersionAttribute, true).Any())
.Select(mappedType => upconverterType.MakeGenericType(mappedType.SourceType, mappedType.DestinationType))
.ToList();
}
}
..........snip.........
//Add EventUpconversion Mapping Profiles here, profiles must be loaded before running the upconverter finder
//Mapper.AddProfile<TypeOfProfile>();
var upconverterTypes = UpConversionFinder.GetUpconverters();
upconverterTypes.ForEach(x => builder.RegisterType(x));
var wireup = Wireup.Init()
.LogTo(x => new EventStoreLogger(LogManager.GetLogger(string.Format("EventStore.{0}", x.Name))))
.UsingSynchronousDispatchScheduler(container.Resolve<IDispatchCommits>())
.UsingSqlPersistence("EventStore")
.WithDialect(new MsSqlDialect())
.InitializeStorageEngine()
.UsingNewtonsoftJsonSerialization(new VersionedEventSerializationBinder())
.Compress()
.UsingEventUpconversion();
foreach (var upconverterType in upconverterTypes)
{
wireup.AddConverter((dynamic)container.Resolve(upconverterType));
}
var store = wireup.Build();
.........snip...........
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment