论坛首页 编程语言技术论坛

关于REST的一点想法,欢迎大家讨论。

浏览 71015 次
该帖已经被评为精华帖
作者 正文
   发表时间:2007-04-12  
AllenYoung 写道

REST到底能把开发简化到什么程度,我看只有经过实际开发才能证明。就好像Rails刚出来的时候叫嚣开发效率比Java高10倍,我们也都嗤之以鼻。但是随着用过Rails的人多了,有经验的人也提出了比较中肯的提高程度,并且也被大家认可了。

Rails的效率是实实在在摆在那里的,不写程序都能看出来,实际开发更是爽呆。而REST完全不能给人这种感觉。我也写了程序试了一下,具体的代码没数,但是肯定不是震撼级的减少。除非对Rails进行进一步的封装。目前generate出来的scaffold resource代码还是有很多重复性的。也许这个工作做好了REST会进一步发展。
AllenYoung 写道

REST的优点就在于它很简单,而具体强大不强大,要看我们怎么使用。最好的东西不是最复杂最强大的,而是最简单最合适的。对REST的理解和运用程度直接影响着开发效率。这就好像OO很好,但是不是所有人都可以用好一样。

简单也得够用啊。没法直接用REST的实在太多了,比如登录,比如注销,比如计算两个人的薪水。我尝试想用REST来做个程序,遇到了太多的阻碍。
0 请登录后投票
   发表时间:2007-04-12  
yuxie 写道

Rails的效率是实实在在摆在那里的,不写程序都能看出来,实际开发更是爽呆。而REST完全不能给人这种感觉。我也写了程序试了一下,具体的代码没数,但是肯定不是震撼级的减少。除非对Rails进行进一步的封装。目前generate出来的scaffold resource代码还是有很多重复性的。也许这个工作做好了REST会进一步发展。

scaffold_resource我也不太喜欢,不过我觉得这是Rails对REST的支持还不是很好。
yuxie 写道

简单也得够用啊。没法直接用REST的实在太多了,比如登录,比如注销,比如计算两个人的薪水。我尝试想用REST来做个程序,遇到了太多的阻碍。

确实是有问题,我从一开始就是说有些困难。但是这些困难是因为REST自身的局限,还是因为我们对它的理解和使用不成熟,这个是需要考虑的问题。
3 请登录后投票
   发表时间:2007-04-12  
yuxie 写道
简单也得够用啊。没法直接用REST的实在太多了,比如登录,比如注销,比如计算两个人的薪水。我尝试想用REST来做个程序,遇到了太多的阻碍。

登录/注销:如果把“会话”看作资源,这两个操作就是“会话”资源的create和delete
“计算两个人的薪水”只是实现方式,也许你可以考虑它背后代表的业务价值是什么,你为什么需要计算这个东西,也许你会发现一个资源正好可以代表它
我并不是说REST可以处理所有情况,但它可以处理的情况也许比我们想象的要多
0 请登录后投票
   发表时间:2007-04-12  
其实实现rest是很容易的. 比如A+B...
rest post
***********
<info>
<A>234</A>
<B>678</B>
</info>
**************
to url---- ws.add

ws.add  运行xquery
******************************
let $doc := request:get-data()
let $A := $doc/A/text()
let $B := $doc/B/text()
let $C := $A + $B /*实际不能这么写*/
    return
   
<info>
{$A}+{$B}={$C}
</info>
****************************
将返回的信息交给ajax处理这样就是完整的rest
0 请登录后投票
   发表时间:2007-04-12  
URL Orientated Design,好想法。以前一直不自觉地在用,AY的文章让我更清楚了。

有这么多名词,都很闪亮。FDD、TDD、BDD,到底应该由什么来驱动开发呢?我想他们针对不同的开发方面。简单的说就是
- Feature Driven Development,功能驱动整个开发流程,一段时间一个迭代。
- Behaviour Driven Design,虽然BDD从表面上看就是TDD的另一种写法,但是因为BDD是用用户的语言,是用来表述feature的,可以用来Design。BDD的产品是spec,有unit spec, functional spec, integration spec,这不就是设计书吗。
- URL Driven Modeling,AY的文章已经说的很清楚了
- Test Driven Coding,不用多说了。
0 请登录后投票
   发表时间:2007-04-12  
winterwolf 写道
其实实现rest是很容易的. 比如A+B...
rest post
***********
<info>
<A>234</A>
<B>678</B>
</info>
**************
to url---- ws.add

ws.add  运行xquery
******************************
let $doc := request:get-data()
let $A := $doc/A/text()
let $B := $doc/B/text()
let $C := $A + $B /*实际不能这么写*/
    return
   
<info>
{$A}+{$B}={$C}
</info>
****************************
将返回的信息交给ajax处理这样就是完整的rest


实现REST是不难,java的servlet一直就有put和delete方法,只是传统的MVC框架不重视,rails充分压榨了HTTP协议,在现有基础上的扩展远比重新定义一个复杂的规范易用得多。
0 请登录后投票
   发表时间:2007-04-12  
   对楼主的帖子有个小小疑问,也是自己没搞懂的。既然REST是Representation State Transfer,楼主又说所有操作都是无状态的,那么当中的State又是什么呢?
   Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.
   从文字中理解, 每个选择的link都是一个state的改变。而点击link对于web系统来说,就是一种操作。看上去和楼主的无状态是矛盾的。
   大家有何想法?
0 请登录后投票
   发表时间:2007-04-12  
凤舞凰扬 写道
   对楼主的帖子有个小小疑问,也是自己没搞懂的。既然REST是Representation State Transfer,楼主又说所有操作都是无状态的,那么当中的State又是什么呢?
   Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.
   从文字中理解, 每个选择的link都是一个state的改变。而点击link对于web系统来说,就是一种操作。看上去和楼主的无状态是矛盾的。
   大家有何想法?
无状态指的应该是“服务器无状态”。
0 请登录后投票
   发表时间:2007-04-12  
gigix 写道

登录/注销:如果把“会话”看作资源,这两个操作就是“会话”资源的create和delete


有道理。可是先不说这给代码可读性带来了什么坏处,这样的抽取,只能增加代码量,不能减少。因为除了原来user下的两个方法的内容拿到了userSession下,还得加一个userSession的类定义。

而大家所谓的能大大减少代码量的REST,应该是generate scaffold resource生成的那些代码。只要所有的resource都是那样的逻辑,比如:
 # GET /products
  # GET /products.xml
  def index
    @products = Product.find(:all)
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @products.to_xml(:include => :categories) }
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @product.to_xml }
    end
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1;edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        flash[:notice] = 'Product was successfully created.'
        format.html { redirect_to product_url(@product) }
        format.xml  { head :created, :location => product_url(@product) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors.to_xml }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = '更新成功!'
        format.html { redirect_to product_url(@product) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors.to_xml }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.xml  { head :ok }
    end
  end

大可以把这些代码抽象出来,以后根本不用generate就能使。可是rails目前没有这么做。

咳:
所以,写着写着,新的结论出来了
能用ActiviRecord表示的东东使用REST很有前途
不能用ActiviRecord表示的东东使用REST很没前途
0 请登录后投票
   发表时间:2007-04-12  
yuxie 写道
gigix 写道

登录/注销:如果把“会话”看作资源,这两个操作就是“会话”资源的create和delete


大可以把这些代码抽象出来,以后根本不用generate就能使。可是rails目前没有这么做。

咳:
所以,写着写着,新的结论出来了
能用ActiviRecord表示的东东使用REST很有前途
不能用ActiviRecord表示的东东使用REST很没前途


你把资源狭义化了,资源 !=ActiveRecord
REST的资源更多的要求的是从资源去观察web操作,而并不是非要操纵ActiviRecord。REST是一种架构,更是一种思考方式
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics