Beispiel #1
0
        /// <summary>
        /// Executes command with result
        /// </summary>
        /// <param name="command">Command</param>
        /// <param name="cancellationToken"></param>
        /// <typeparam name="TResult"></typeparam>
        /// <returns>Command result</returns>
        public async Task <TResult> ProcessAsync <TResult>(ICommand <TResult> command,
                                                           CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var commandType = command.GetType();

            if (!_commandHandlerTypesCache.TryGetValue(commandType, out var commandHandlerType))
            {
                commandHandlerType = typeof(ICommandHandler <,>).MakeGenericType(commandType, typeof(TResult));
                _commandHandlerTypesCache.TryAdd(commandType, commandHandlerType);
            }

            var contextConstructor = CommandContextCtorFactory.GetOrCreate(commandType, typeof(TResult));
            var context            = contextConstructor.Construct(command, commandHandlerType, cancellationToken);

            await _commandPipe.Send(context);

            var resultingContext = (IResultingHandlingContext)context;

            return(((Task <TResult>)resultingContext.ResultTask).Result);
        }
Beispiel #2
0
        ///<summary>
        /// Executes command without result
        /// </summary>
        /// <param name="command">Command</param>
        /// <param name="cancellationToken"></param>
        public Task ProcessAsync(ICommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var commandType = command.GetType();

            if (!_commandHandlerTypesCache.TryGetValue(commandType, out var commandHandlerType))
            {
                commandHandlerType = typeof(ICommandHandler <>).MakeGenericType(commandType);
                _commandHandlerTypesCache.TryAdd(commandType, commandHandlerType);
            }

            var contextConstructor = CommandContextCtorFactory.GetOrCreate(commandType, null);
            var context            = contextConstructor.Construct(command, commandHandlerType, cancellationToken);

            return(_commandPipe.Send(context));
        }