示例#1
0
        public Result <SaveBookCommandResult, SaveBookCommandError> SaveBook(SaveBookCommand command)
        {
            Result <SaveBookCommandResult, SaveBookCommandError> _cmdResult;

            try  // try dispatch of the command
            {
                _cmdResult =
                    _CommandDispatcher.Dispatch <SaveBookCommandResult, SaveBookCommandError>(_Context, command); //handle the request
            }
            catch (Exception _exception)                                                                          // catch every exception
            {
                string errorMsg = $"Error in {this.GetType()} commandHandler. Message: {_exception.Message} \n Stacktrace: {_exception.StackTrace}";
                _Log.ErrorFormat(errorMsg);
                return(Result.Fail <SaveBookCommandResult, SaveBookCommandError>(SaveBookCommandError.Set_InternalServerError(errorMsg)));  // return internal server error
            }
            finally
            {
                // do nothing
            }

            //SaveBookCommandHandler handler = new SaveBookCommandHandler();
            //return handler.Handle(_Context, cmd);

            return(_cmdResult);
        }
示例#2
0
        private bool Handle(SaveBookCommand command)
        {
            var provider = DependencyInjectorStub.Get((s, c) =>
            {
                BootStrapper.RegisterServices(s, c);
                s.AddScoped(x => MockRepository.GetContext());
                s.AddScoped <IBusPublisher>(x => Bus);
            });

            var handler = provider.GetRequiredService <IRequestHandler <SaveBookCommand, bool> >();

            return(handler.Handle(command, CancellationToken.None).GetAwaiter().GetResult());
        }
        public void CommandDispatcher_TypeError__Test()
        {
            #region preparation
            ApplicationContext _Context           = new ApplicationContext();
            CommandDispatcher  _CommandDispatcher = new CommandDispatcher();

            SaveBookCommand command = new SaveBookCommand("titolo", true);   // init command
            #endregion

            #region test error
            Assert.ThrowsException <InvalidOperationException>(
                () => _CommandDispatcher.Dispatch <SaveBookCommandResult, WrongClass>(_Context, command)
                );  // call the dispatcher with the wrong class
            #endregion

            #region test success
            _CommandDispatcher.Dispatch <SaveBookCommandResult, SaveBookCommandError>(_Context, command);  // call the dispatcher with the right class
            #endregion
        }
示例#4
0
        public void CommandHandler_InterceptCommandExecuted2Times_Test()
        {
            #region preparation
            ApplicationContext _Context         = new ApplicationContext();
            string             errorType        = SaveBookCommandError.Set_InternalServerError("").ErrorType;
            string             errorDetail_part = "command already processed";
            #endregion

            #region test error
            SaveBookCommand command = new SaveBookCommand("titolo1", true); // command to execute 2 times
            Result <SaveBookCommandResult, SaveBookCommandError> result =
                new SaveBookCommandHandler(_Context).Handle(command);       // first execution, success
            Assert.IsTrue(result.IsSuccess);

            result = new SaveBookCommandHandler(_Context).Handle(command);  // second execution, failure

            Assert.AreEqual(errorType, result.Error.ErrorType);
            Assert.IsTrue(result.Error.ErrorDetail.Contains(errorDetail_part));
            #endregion
        }
示例#5
0
        public async Task <IActionResult> Save(SaveBookCommand book)
        {
            await _mediator.Send(book);

            return(Ok());
        }
示例#6
0
 public Result <SaveBookCommandResult, SaveBookCommandError> SaveBook(SaveBookCommand command)
 {
     return(new SaveBookCommandHandler(_Context).Handle(command));
 }
示例#7
0
 public async Task <IActionResult> UpdateBooks(long id, SaveBookCommand command)
 {
     command.Id = id;;
     return(Ok(await _mediator.Send(command, Request.HttpContext.RequestAborted)));
 }
示例#8
0
 public async Task <IActionResult> CreateBooks(SaveBookCommand command)
 {
     return(Ok(await _mediator.Send(command, Request.HttpContext.RequestAborted)));
 }