public void ExecuteCommandPayload(ICommandPayload commandPayload, Action onCompleteCallback) { var commandPayloadType = commandPayload.GetType(); foreach (var methodHandlersPair in _methodHandlersByContextType) { if (!methodHandlersPair.Value.ContainsKey(commandPayloadType)) { continue; } if (!_invocationTargets.TryGetValue(methodHandlersPair.Key, out ICommandHandlerContext handlerContext) || handlerContext == null) { continue; } ExecuteCommandPayload(handlerContext, commandPayload, methodHandlersPair.Value[commandPayloadType], onCompleteCallback); return; } throw new Exception($"No command handler for command payload: {commandPayloadType.Name}."); }
private void ExecuteCommandPayload(ICommandHandlerContext handlerContext, ICommandPayload commandPayload, MethodInfo methodHandler, Action onCompleteCallback) { var handlerContextType = handlerContext.GetType(); var commandPayloadType = commandPayload.GetType(); var commandType = typeof(Command <>); Type[] typeArgs = { commandPayloadType }; commandType = commandType.MakeGenericType(typeArgs); var command = (ICommand)Activator.CreateInstance( commandType, new object[] { commandPayload, CreateQueuedCompletionCallback( $"{handlerContextType.Name}.{methodHandler.Name}", onCompleteCallback ) }); command.Execute(handlerContext, methodHandler); }
public void ExecuteCommandPayload(ICommandHandlerContext handlerContext, ICommandPayload commandPayload, Action onCompleteCallback) { var handlerContextType = handlerContext.GetType(); if (_methodHandlersByContextType.TryGetValue(handlerContextType, out Dictionary <Type, MethodInfo> methodHandlers)) { var commandPayloadType = commandPayload.GetType(); if (methodHandlers.ContainsKey(commandPayloadType)) { ExecuteCommandPayload(handlerContext, commandPayload, methodHandlers[commandPayloadType], onCompleteCallback); } else { throw new Exception($"No command handler for command payload: {commandPayloadType.Name} on type: {handlerContextType.Name}"); } } else { throw new Exception($"No command handlers registered for type: {handlerContextType.Name}"); } }