Ejemplo n.º 1
0
        private void ExecuteCommand <TCommand>(TCommand command, CommandModel commandModel)
            where TCommand : Command
        {
            IExecute <TCommand> handler = null;

            try
            {
                handler = (IExecute <TCommand>)_serviceFactory.Build(typeof(IExecute <TCommand>));

                _logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);

                _commandQueueManager.Start(commandModel);
                BroadcastCommandUpdate(commandModel);

                if (ProgressMessageContext.CommandModel == null)
                {
                    ProgressMessageContext.CommandModel = commandModel;
                }

                handler.Execute(command);

                _commandQueueManager.Complete(commandModel, command.CompletionMessage ?? commandModel.Message);
            }
            catch (CommandFailedException ex)
            {
                _commandQueueManager.SetMessage(commandModel, "Failed");
                _commandQueueManager.Fail(commandModel, ex.Message, ex);
                throw;
            }
            catch (Exception ex)
            {
                _commandQueueManager.SetMessage(commandModel, "Failed");
                _commandQueueManager.Fail(commandModel, "Failed", ex);
                throw;
            }
            finally
            {
                BroadcastCommandUpdate(commandModel);

                _eventAggregator.PublishEvent(new CommandExecutedEvent(commandModel));

                if (ProgressMessageContext.CommandModel == commandModel)
                {
                    ProgressMessageContext.CommandModel = null;
                }

                if (handler != null)
                {
                    _logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, commandModel.Duration.ToString());
                }
            }
        }
Ejemplo n.º 2
0
        public T Resolve <T>() where T : class
        {
            object resolvedInstance = _instanceMap.GetOrAdd(typeof(T), InstanceFactory);

            if (resolvedInstance != null)
            {
                return((T)resolvedInstance);
            }
            if (_defaultServiceFactory == null)
            {
                throw new Exception("IoC container could not resolve type " + typeof(T).Name);
            }
            return(_defaultServiceFactory.Build <T>());
        }
Ejemplo n.º 3
0
        private void ExecuteCommand <TCommand>(Command command) where TCommand : Command
        {
            var handlerContract = typeof(IExecute <>).MakeGenericType(command.GetType());
            var handler         = (IExecute <TCommand>)_serviceFactory.Build(handlerContract);

            _logger.Trace("{0} -> {1}", command.GetType().Name, handler.GetType().Name);

            try
            {
                _trackCommands.Start(command);
                BroadcastCommandUpdate(command);

                if (!MappedDiagnosticsContext.Contains("CommandId") && command.SendUpdatesToClient)
                {
                    MappedDiagnosticsContext.Set("CommandId", command.Id.ToString());
                }

                handler.Execute((TCommand)command);

                if (command.State == CommandStatus.Running)
                {
                    _trackCommands.Completed(command);
                }
            }
            catch (Exception e)
            {
                _trackCommands.Failed(command, e);
                throw;
            }
            finally
            {
                BroadcastCommandUpdate(command);
                _eventAggregator.PublishEvent(new CommandExecutedEvent(command));

                if (MappedDiagnosticsContext.Get("CommandId") == command.Id.ToString())
                {
                    MappedDiagnosticsContext.Remove("CommandId");
                }
            }

            _logger.Trace("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, command.Runtime.ToString(""));
        }