Пример #1
0
        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}");
        }
Пример #2
0
        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}");
        }
Пример #3
0
        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));
        }
Пример #4
0
        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));
        }
Пример #5
0
        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
        }
Пример #6
0
        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
            }));
        }
Пример #7
0
        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
        }
Пример #8
0
        /*
         * 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);
        }
Пример #9
0
 private CommandLog(ICommandBase command, Exception exception, bool operationSuccess)
     : base(exception, operationSuccess)
 {
     this.SerializedCommand = command.ToSerialize();
     this.CommandType       = command.GetType().Name;
 }