public bool IsAuthenticationRequired(ICommandBase command) { if (commandTypePermissions.Value.TryGetValue(command.GetType(), out var info)) { return(info.IsAuthenticationRequired); } throw new ArgumentException( $"Unknown command type passed to IsAuthenticationRequired: {command.GetType().FullName}"); }
public IReadOnlyCollection <Permission> GetCommandPermissions(ICommandBase command) { if (commandTypePermissions.Value.TryGetValue(command.GetType(), out var permissions)) { return(permissions); } throw new ArgumentException( $"Unknown command type to GetCommandPermissions for: {command.GetType().FullName}"); }
public Task SendAsync(ICommandBase command, CancellationToken cancellationToken) { Type genericCommandType = command.GetType().GetInterfaces().FirstOrDefault( x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(ICommand <>)); Type handlerType; if (genericCommandType != null) { handlerType = typeof(ICommandHandler <,>).MakeGenericType(command.GetType(), genericCommandType.GetGenericArguments()[0]); return(InvokeHandleAsync(handlerType, command, cancellationToken)); } handlerType = typeof(ICommandHandler <>).MakeGenericType(command.GetType()); return(InvokeHandleAsync(handlerType, command, cancellationToken)); }
public Task SendAsync(ICommandBase command, CommandExecutionOptions executionOptions, CancellationToken cancellationToken = default(CancellationToken)) { var commandBus = commandRouter.FindRoute(command.GetType()) ?? throw new ArgumentException($"No route to a command bus found for command type {command.GetType()}"); return(commandBus.SendAsync(command, executionOptions, cancellationToken)); }
private Task InvokeHandleAsync(Type handlerType, ICommandBase command, CancellationToken cancellationToken) { Type commandType = command.GetType(); object listener = serviceLocator.Get(handlerType); var handleMethod = listener.GetType() .GetRuntimeMethod(nameof(ICommandHandler <ICommand> .HandleAsync), new[] { commandType, typeof(CancellationToken) }); return((Task)handleMethod.Invoke(listener, new object[] { command, cancellationToken })); // TODO test with contravariant handlers }
public Task <object> ProcessAsync(ICommandBase command, CommandBusMiddlewareDelegate executionHandler, ICommandBus commandBus, CommandExecutionOptions executionOptions, CancellationToken cancellationToken) { var processMethod = GetType().GetRuntimeMethods().Single(x => x.Name == "ProcessInternalAsync"); var boundProcessMethod = processMethod.MakeGenericMethod(command.GetType()); return((Task <object>)boundProcessMethod.Invoke(this, new object[] { command, executionHandler, commandBus, executionOptions, cancellationToken })); }
private async Task <TResult> InvokeHandleAsync <TResult>(Type handlerType, ICommandBase command, CommandExecutionOptions executionOptions, CancellationToken cancellationToken) { CommandBusMiddlewareDelegate executionHandler = async processedCommand => { if (processedCommand == null) { throw new ArgumentNullException(nameof(processedCommand)); } if (!command.GetType().IsInstanceOfType(processedCommand)) { throw new ArgumentException( $"Command passed back to command bus from a middleware is not of its original type {command.GetType()}"); } Type commandType = command.GetType(); object listener = serviceLocator.Get(handlerType); var handleMethod = listener.GetType() .GetRuntimeMethod(nameof(ICommandHandler <ICommand> .HandleAsync), new[] { commandType, typeof(CancellationToken) }); var resultTask = (Task)handleMethod.Invoke(listener, new object[] { command, cancellationToken }); if (resultTask is Task <TResult> resultObjectTask) { return(await resultObjectTask); } else { await resultTask; return(null); } }; object result = await pipeline.ProcessAsync(command, executionHandler, this, executionOptions, cancellationToken); return((TResult)result); // TODO test with contravariant handlers }
/* * Return. */ public void ReturnCommand(ICommandBase command) { if (command == null) { return; } var commandType = command.GetType(); var usedInstances = GetCommandUsedInstances(commandType, false); if (usedInstances == null || !usedInstances.Remove(command)) { return; } GetCommandAvailableInstances(commandType, false).Push(command); }
private CommandLog(ICommandBase command, Exception exception, bool operationSuccess) : base(exception, operationSuccess) { this.SerializedCommand = command.ToSerialize(); this.CommandType = command.GetType().Name; }