Exemplo n.º 1
0
        /// <summary>
        /// Processes the specified command.
        /// </summary>
        /// <typeparam name="TCommand">The type of the command.</typeparam>
        /// <param name="command">The command.</param>
        /// <exception cref="CommandHandlerNotFoundException"></exception>
        public void Process <TCommand>(TCommand command) where TCommand : ICommand
        {
            Validator.ValidateObject(command, new ValidationContext(command, null, null), true);

            //var handlers = ServiceLocator.Current.GetAllInstances<ICommandHandler<TCommand>>();
            var handlers = _factory.GetHandlers <TCommand>();

            if (handlers == null || !handlers.Any())
            {
                throw new CommandHandlerNotFoundException(typeof(TCommand));
            }

            foreach (var handler in handlers)
            {
                try
                {
                    handler.Handle(command);
                }
                finally
                {
                    _factory.ReleaseHandler(handler);
                }
            }
        }