public static void MapEventHandler(IBoundedContextModel contextMap, Type eventHandlerType) { var methods = eventHandlerType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); foreach (var method in methods.Where(m => m.Name == "When")) { var parameters = method.GetParameters(); if (parameters.Count() == 1 && contextMap.IsEventType(parameters[0].ParameterType)) { if (method.ReturnType != null && contextMap.IsCommandType(method.ReturnType)) { CallGenericMethod(nameof(ApplyProcessHandlerToContextMap), contextMap, eventHandlerType, method.ReturnType, parameters); } else { CallGenericMethod(nameof(ApplyEventHandlerToContextMap), contextMap, eventHandlerType, typeof(void), parameters); } } } }
public IEnumerable <IEvent> ProcessCommand(ICommand command) { var results = new List <IEvent>(); if (command == null) { throw new ArgumentNullException("command"); } if (!this.commandHistory.Contains(command.Id)) { var stateChange = this.DispatchCommand(command); if (stateChange != null) { Type typeInfo = stateChange.GetType(); /* if the return type is a recogised event then emit it as an event */ if (this.contextMap.IsEventType(typeInfo)) { this.AddStateChangeToResultingEvents(command, results, stateChange); } /* if any members of the returned class are recognised events then emit them as seperate events */ /* this is primarily to support returning multiple events from a single aggregate call in a tuple like format */ typeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => contextMap.IsEventType(p.PropertyType)) .Select(p => p.GetValue(stateChange)) .Where(v => v != null) .ToList() .ForEach(s => this.AddStateChangeToResultingEvents(command, results, s)); } } this.uncommittedChanges.AddRange(results); return(results); }