Exemple #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);
        }
Exemple #2
0
        public void CommandHandler_Exception_Test()
        {
            #region preparation
            ApplicationContext _Context         = new ApplicationContext();
            string             errorType        = SaveBookCommandError.Set_InternalServerError("").ErrorType;
            string             errorDetail_part = "Object reference not set to an instance of an object";
            #endregion

            #region test error
            Result <SaveBookCommandResult, SaveBookCommandError> result =
                new SaveBookCommandHandler(_Context).Handle(null);

            Assert.AreEqual(errorType, result.Error.ErrorType);
            Assert.IsTrue(result.Error.ErrorDetail.Contains(errorDetail_part));
            #endregion
        }
Exemple #3
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
        }