private void ExecuteCommandHandler(string commandName, Action <object> commandHandler) { var systemCommandType = string.IsNullOrWhiteSpace(commandName) ? typeof(HelpCommand) : _systemRegistry.Find(commandName); var systemCommandInstance = _systemCommandFactory.Resolve(systemCommandType); if (systemCommandInstance != null) { try { commandHandler(systemCommandInstance); } finally { _systemCommandFactory.Release(systemCommandInstance); } return; } var commandType = _registry.Find(commandName); if (commandType == null) { throw new NotSupportedException(string.Format("The command \"{0}\" is not currently supported.", commandName)); } var commandInstance = _commandFactory.Resolve(commandType); if (commandInstance == null) { throw new Exception(string.Format("Command factory \"{1}\" was unable to resolve an instance for command type \"{0}\", it returned null instead.", commandType, _commandFactory.GetType().FullName)); } try { commandHandler(commandInstance); } finally { _commandFactory.Release(commandInstance); } }