public virtual void Register(IContainsEventAggregate aggregate) { if (aggregate == null) { throw new ArgumentNullException("aggregate"); } _registered = aggregate; // Get instance methods named Apply with one parameter returning void var applyMethods = aggregate.GetType() .GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(m => m.Name == "When" && m.GetParameters().Length == 1 && m.ReturnParameter.ParameterType == typeof(void)) .Select(m => new { Method = m, MessageType = m.GetParameters().Single().ParameterType }); foreach (var apply in applyMethods) { var applyMethod = apply.Method; _handlers.Add(apply.MessageType, m => applyMethod.Invoke(aggregate, new[] { m as object })); } }
public static void ThrowHandlerNotFound(this IContainsEventAggregate aggregate, object eventMessage) { var exceptionMessage = "Aggregate of type '{0}' raised an event of type '{1}' but not handler could be found to handle the message." .FormatWith(aggregate.GetType().Name, eventMessage.GetType().Name); throw new HandlerForDomainEventNotFoundException(exceptionMessage); }
public ConventionEventRouter(bool throwOnApplyNotFound, IContainsEventAggregate aggregate) : this(throwOnApplyNotFound) { Register(aggregate); }