public static dynamic ConcreteGeneric(this IEventWrapper abst, Type?type = null) { type ??= abst.GetContent().GetType(); var ex = typeof(EventWrapperExtensions); var mi = ex.GetMethod("Concrete"); var miConstructed = mi?.MakeGenericMethod(type); object[] args = { abst }; return(miConstructed?.Invoke(null, args) ?? throw new Exception()); }
internal void LoadFromHistory(IEventWrapper eventWrapper) { Version++; if (eventWrapper.Version != Version) { throw new Exception(); } When(eventWrapper.GetContent()); }
public static IEventWrapper <T> Concrete <T>(this IEventWrapper abst) where T : IEvent { var content = abst.GetContent(); if (content is T conc) { return(new EventWrapper <T>(conc, abst.Id, abst.AggregateId, abst.Timestamp, abst.Version)); } throw new Exception(); }
public async Task Publish(IEventWrapper @event, CancellationToken cancellationToken = default) { var eventName = @event.GetContent().GetType().Name; var aggregateId = @event.AggregateId; var version = @event.Version; Console.WriteLine($"processing event of type {eventName} with id {aggregateId} and version {version}"); try { await _eventBus.Publish(@event, cancellationToken); Console.WriteLine($"successfully processed event of type {eventName} with id {aggregateId} and version {version}"); } catch (Exception) { Console.WriteLine($"something went wrong while processing event of type {eventName} with id {aggregateId} and version {version}"); throw; } }
public async Task Publish(IEventWrapper wrapper, CancellationToken cancellationToken = default) { using var scope = _serviceProvider.CreateScope(); var eventType = wrapper.GetContent().GetType(); var eventInterfaceTypes = eventType .GetInterfaces() .Where(i => i.GetInterface(nameof(IEvent)) != null); foreach (var eventInterfaceType in eventInterfaceTypes) { var handlerType = typeof(IHandleEvent <>).MakeGenericType(eventInterfaceType); var concrete = wrapper.ConcreteGeneric(eventInterfaceType); var handlers = scope.ServiceProvider.GetServices(handlerType).Cast <dynamic>(); foreach (var handler in handlers) { await handler.Handle(concrete, cancellationToken); } } }