Ejemplo n.º 1
0
        public CommandResult <TResult> Handle <TCommand, TResult>(
            TCommand command,
            CommandContext context)
            where TCommand : ICommand <TResult>
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Logger.Log(new CommandReceivedLog(command, context));

            using (_scopeFactory())
            {
                try
                {
                    using (var transaction = new TransactionScope())
                    {
                        var validationErrors = _validationFacade.Validate(command);

                        if (validationErrors != null)
                        {
                            return(new CommandResult <TResult> {
                                ValidationErrors = validationErrors
                            });
                        }

                        var commandHandler = _commandHandlerResolver.Resolve <TCommand, TResult>();
                        var result         = commandHandler.Handle(command, context);

                        transaction.Complete();

                        return(new CommandResult <TResult> {
                            Result = result
                        });
                    }
                }
                catch (KnownException e)
                {
                    return(new CommandResult <TResult> {
                        KnownError = e
                    });
                }
                catch (Exception e)
                {
                    return(new CommandResult <TResult> {
                        UnknownError = e
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public CommandResult HandleCommand <TCommand>(TCommand command)
            where TCommand : ICommand
        {
            var validationError = _validationFacade.Validate(command);

            if (validationError != null)
            {
                return(new CommandResult {
                    ValidationError = validationError
                });
            }

            var handler = (ICommandHandler <TCommand>)_commandHandlers[typeof(TCommand)];

            handler.Handle(command);

            return(new CommandResult());
        }
Ejemplo n.º 3
0
 /// <exclude />
 public static ValidationResults Validate <T>(T data)
     where T : class, IData
 {
     return(_implementation.Validate <T>(data));
 }