public static Task HandleMeWith(this IDomainEvent domainEvent, IHandleEvents eventHandler) { var handlerType = eventHandler.GetType(); var eventType = domainEvent.GetType(); var handlerMethodsForEventType = from method in handlerType.GetTypeInfo().DeclaredMethods from parameter in method.GetParameters() where method.IsPublic where parameter.ParameterType == eventType select method; var allHandlerMethods = handlerMethodsForEventType.ToList(); if (handlerMethodsForEventType.Any() == false) { throw new ArgumentException($"This Event Type {eventType.Name} has no Handler Method"); } if (handlerMethodsForEventType.Count() > 1) { throw new ArgumentException($"This Event Type {eventType.Name} has to much Handler Methods"); } // Invoke the one and only Handle([Event Type]) Method return((Task)handlerMethodsForEventType .Single() .Invoke(eventHandler, new object?[] { domainEvent })); }
public static void HandleMeWith(this IAmAnEventMessage @event, IHandleEvents handler) { var handlerType = handler.GetType (); var eventType = @event.GetType (); var eventHandlerMethods = from method in handlerType.GetTypeInfo().DeclaredMethods from parameter in method.GetParameters () where parameter.ParameterType == eventType select method; var allMethodes = eventHandlerMethods.ToList (); if (allMethodes.Any ()) { if (allMethodes.Count () > 1) { throw new MultipleHandlerMethodesDetected (eventType.Name); } allMethodes.First ().Invoke (handler, new object[]{ @event }); } }
public static void HandleMeWith(this IAmAnEventMessage @event, IHandleEvents handler) { var handlerType = handler.GetType(); var eventType = @event.GetType(); var eventHandlerMethods = from method in handlerType.GetTypeInfo().DeclaredMethods from parameter in method.GetParameters() where parameter.ParameterType == eventType select method; var allMethodes = eventHandlerMethods.ToList(); if (allMethodes.Any()) { if (allMethodes.Count() > 1) { throw new MultipleHandlerMethodesDetected(eventType.Name); } allMethodes.First().Invoke(handler, new object[] { @event }); } }