2012-02-13 7 views
0

以下のコードは、インターフェイスとリモートオブジェクトが同じファイルにコード化されていることを示しています。Flash Builderでリモートオブジェクトとインターフェイスを分離する方法Flex

現在のコード:やっての

<s:RemoteObject id="ro" 
        destination="customerService" 
        source="customerService" 
        endpoint="http://localhost/amfphp/gateway.php" 
        showBusyCursor="true"> 
     <s:method name="getCustomer" result="getCustomer_resultHandler(event)"> 
      <s:arguments> 
       <CusOpt>{''}</CusOpt> 
       <option>{''}</option> 
       <idcompany>{2}</idcompany> 
      </s:arguments> 
     </s:method> 
     <s:method name="genPKID" result="genPKID_resultHandler(event)"> 
      <s:arguments> 
       <idcompany>{2}</idcompany> 
      </s:arguments> 
     </s:method> 
    </s:RemoteObject> 

間違った方法:

import mx.rpc.remoting.RemoteObject; 


    public class CustomerRO extends RemoteObject 
    { 
     public function CustomerRO(destination:String=null) 
     { 
      super(destination); 
      this.destination = "customerService"; 
      this.source = "customerService"; 
      this.endpoint = "http://localhost/amfphp/gateway.php"; 
      this.showBusyCursor = true; 
     } 
    } 

答えて

2

あなたはあなたのリモートサービスのクライアント側のサービスのスタブを作成します。この設定を適切に行うには、いくつかの手順があります。

簡潔にするためのサービス・インターフェース

を作成し、私たちはただひとつの方法とのインタフェースを作成しますが、必要な数を追加することができます。

package be.vmm.user.service { 
    import mx.rpc.AsyncToken; 

    public interface ICustomerService{  
     function getCustomerById(id:int):AsyncToken;   
    } 
} 

あなたがRemoteObjectのを拡張しているあなたの例ではインターフェース

の実装を作成します。私はそれをカプセル化することをお勧めします:それははるかに柔軟性があります。あなたのコードでは、接続情報がハードコーディングされているので、この情報が変更されるたびにアプリケーションを再コンパイルする必要があります。

public class CustomerService implements ICustomerService { 
    private var ro:RemoteObject; 

    public function CustomerService(ro:RemoteObject) { 
     this.ro = ro; 
    } 

    public function getCustomerById(id:int):AsyncToken { 
     return ro.getCustomerById(id); 
    } 

} 

また、インターフェイスの別の実装を作成することもできます。最も一般的な使用例は、サーバーに実際には接続せず、偽のデータを直接返すモックサービスを作成することです。サーバ接続なしでアプリケーションをテストする場合は、実際のサービススタブをモックサービススタブに置き換えることができます。これは両方とも同じインターフェイスを実装するためです。

使用実装

var ro:RemotObject = new RemoteObject(); 
ro.destination = "customerService"; 
ro.source = "customerService"; 
ro.endpoint = "http://localhost/amfphp/gateway.php"; 
ro.showBusyCursor = true; 
//these properties are best externalized in a configuration file 

var service:ICustomerService = new CustomerService(ro); 
var token:ASyncToken = service.getCustomerById(1); 
token.addResponder(new Responder(handleResult, handleFault)); 

private function handleResult(event:ResultEvent):void { 
    //do what you need to do 
    trace(event.result as Customer); 
} 

private function handleFault(event:FaultEvent):void { 
    //show error message 
}