Esempio n. 1
0
        CommandResult Handle(ITransaction transaction, ICommand command)
        {
            var commandResult = new CommandResult();
            try
            {
                using (_localizer.BeginScope())
                {
                    commandResult = CommandResult.ForCommand(command);

                    var authorizationResult = _commandSecurityManager.Authorize(command);
                    if (!authorizationResult.IsAuthorized)
                    {
                        commandResult.SecurityMessages = authorizationResult.BuildFailedAuthorizationMessages();
                        transaction.Rollback();
                        return commandResult;
                    }

                    var validationResult = _commandValidationService.Validate(command);
                    commandResult.ValidationResults = validationResult.ValidationResults;
                    commandResult.CommandValidationMessages = validationResult.CommandErrorMessages;

                    if (commandResult.Success)
                    {
                        try
                        {
                            _commandHandlerManager.Handle(command);
                            transaction.Commit();
                        }
                        catch (TargetInvocationException ex)
                        {
                            _exceptionPublisher.Publish(ex);
                            commandResult.Exception = ex.InnerException;
                            transaction.Rollback();
                        }
                        catch (Exception ex)
                        {
                            _exceptionPublisher.Publish(ex);
                            commandResult.Exception = ex;
                            transaction.Rollback();
                        }
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                _exceptionPublisher.Publish(ex);
                commandResult.Exception = ex.InnerException;
            }
            catch (Exception ex)
            {
                _exceptionPublisher.Publish(ex);
                commandResult.Exception = ex;
            }

            return commandResult;
        }
 public static void FromCommandResult(this ModelStateDictionary modelStateDictionary, CommandResult commandResult, string exceptionErrorName)
 {
     if (commandResult.Invalid)
     {
         modelStateDictionary.AddToModelErrors(commandResult.ValidationResults);
     }
     else
     {
         modelStateDictionary.AddModelError(exceptionErrorName, commandResult.Exception.Message);
     }
 }
        /// <summary>
        /// Add command result information onto a <see cref="ModelStateDictionary"/>
        /// </summary>
        /// <param name="modelStateDictionary"><see cref="ModelStateDictionary"/> to add to</param>
        /// <param name="commandResult"><see cref="CommandResult"/> to add from</param>
        /// <param name="exceptionErrorName">Optional parameter for specifying a error name for the any exception, if not specified the error name will be the name of the exception</param>
        public static void FromCommandResult(this ModelStateDictionary modelStateDictionary, CommandResult commandResult, string exceptionErrorName = null)
        {
            if (commandResult.Invalid)
            {
                modelStateDictionary.AddToModelErrors(commandResult.ValidationResults);

                if( commandResult.CommandValidationMessages != null ) 
                    modelStateDictionary.AddToModelErrors(commandResult.CommandValidationMessages);
            }
            else
            {
                if (exceptionErrorName == null)
                    exceptionErrorName = commandResult.Exception.GetType().Name;

                modelStateDictionary.AddModelError(exceptionErrorName, commandResult.Exception.Message);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Merges this instance of a CommandResult with another
        /// </summary>
        /// <param name="commandResultToMerge">The <see cref="CommandResult"/> to merge with the current instance</param>
        public void MergeWith(CommandResult commandResultToMerge)
        {
            if (Exception == null)
                Exception = commandResultToMerge.Exception;

            if (commandResultToMerge.ValidationResults == null) 
                return;

            if (ValidationResults == null)
            {
                ValidationResults = commandResultToMerge.ValidationResults;
                return;
            }

            var validationResults = ValidationResults.ToList();
            validationResults.AddRange(commandResultToMerge.ValidationResults);
            ValidationResults = validationResults.ToArray();
        }
Esempio n. 5
0
        public CommandResult Handle(ICommand command)
        {
            var validationResults = new ObservableCollection<ValidationResult>();
            var commandValidationMessages = new ObservableCollection<string>();
            var commandResult = new CommandResult();
            commandResult.CommandName = command.Name;
            commandResult.ValidationResults = validationResults;
            commandResult.CommandValidationMessages = commandValidationMessages;

            if( !command.GetType().Equals(typeof(Command)) )
                CopyPropertyValuesToParameters(command);

            var descriptor = new CommandDescriptor
            {
                Id = Guid.NewGuid(),
                Name = command.Name,
                Command = _serializer.ToJson(command.Parameters),
                GeneratedFrom = command.GetType().FullName
            };
            _commands[descriptor.Id] = command;
            command.IsBusy = true;
            command.IsProcessing = true;
            _proxy.Handle(descriptor).ContinueWith(a =>
            {
                foreach (var commandValidationMessage in a.Result.CommandValidationMessages)
                    commandValidationMessages.Add(commandValidationMessage);
                foreach (var validationResult in a.Result.ValidationResults)
                    validationResults.Add(validationResult);

                commandResult.Exception = a.Result.Exception;
                commandResult.ExceptionMessage = a.Result.ExceptionMessage;

                command.IsBusy = false;

                if( command is INotifyCommandResultsReceived ) 
                    ((INotifyCommandResultsReceived)command).OnCommandResultsReceived(descriptor.Id, commandResult);
            });

            return commandResult;
        }
Esempio n. 6
0
        public CommandResult Handle(ICommand command)
        {
            var validationResult = new ObservableCollection<ValidationResult>();
            var commandValidationMessages = new ObservableCollection<string>();
            var commandResult = new CommandResult();
            commandResult.ValidationResults = validationResult;
            commandResult.CommandValidationMessages = commandValidationMessages;

            var client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Encoding = Encoding.UTF8;

            var source = Application.Current.Host.Source;
            var url = string.Format("{0}://{1}{2}/CommandCoordinator/Handle",
                source.Scheme,
                source.Host,
                source.Port == 80 ? string.Empty : ":" + source.Port);

            dynamic parameters = new ExpandoObject();
            dynamic commandDescriptor = new ExpandoObject();
            commandDescriptor.Name = "TestCommand";
            commandDescriptor.Command = _serializer.ToJson(new { });

            parameters.commandDescriptor = _serializer.ToJson(commandDescriptor);

            var json = _serializer.ToJson(parameters);

            
            var uri = new Uri(url, UriKind.Absolute);
            client.UploadStringAsync(uri, "POST", json);
            client.UploadStringCompleted += (s, e) =>
                {
                    var actualCommandResult = _serializer.FromJson<CommandResult>(e.Result);
                    var i = 0;
                    i++;
                };
            return commandResult;
        }
Esempio n. 7
0
        /// <summary>
        /// Merges another CommandResult instance into the current instance
        /// </summary>
        /// <param name="commandResultToMerge">The source <see cref="CommandResult"/> to merge into current instance</param>
        public void MergeWith(CommandResult commandResultToMerge)
        {
            if (Exception == null)
                Exception = commandResultToMerge.Exception;

            MergeValidationResults(commandResultToMerge);
            MergeCommandErrorMessages(commandResultToMerge);
        }
Esempio n. 8
0
        void MergeCommandErrorMessages(CommandResult commandResultToMerge)
        {
            if (commandResultToMerge.CommandValidationMessages == null)
                return;

            if (CommandValidationMessages == null)
            {
                CommandValidationMessages = commandResultToMerge.CommandValidationMessages;
                return;
            }

            var commandErrorMessages = CommandValidationMessages.ToList();
            commandErrorMessages.AddRange(commandResultToMerge.CommandValidationMessages);
            CommandValidationMessages = commandErrorMessages.ToArray();
        }
Esempio n. 9
0
        void MergeValidationResults(CommandResult commandResultToMerge)
        {
            if (commandResultToMerge.ValidationResults == null)
                return;

            if (ValidationResults == null)
            {
                ValidationResults = commandResultToMerge.ValidationResults;
                return;
            }

            var validationResults = ValidationResults.ToList();
            validationResults.AddRange(commandResultToMerge.ValidationResults);
            ValidationResults = validationResults.ToArray();
        }
Esempio n. 10
0
 public void OnCommandResultsReceived(Guid commandContextId, CommandResult result)
 {
     if (CommandResultsReceived != null)
         Deployment.Current.Dispatcher.BeginInvoke(() => CommandResultsReceived(commandContextId, result));
 }