如何在Flash Builder Flex中分离远程对象和接口 [英] How to separate remote object and interface in Flash Builder Flex

查看:116
本文介绍了如何在Flash Builder Flex中分离远程对象和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码显示接口和远程对象被编码在同一文件中.如何将它们分成两个文件?

The code below shows interface and remote object are coded in the same file How to separate them into two files ?

当前代码:

    <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;
        }
    }

推荐答案

您要为远程服务创建客户端服务存根.有一些步骤可以正确进行此设置.

You want to create a client-side service stub for your remote service. There's a few steps to get this set up properly.

创建服务界面

为简便起见,我们将仅用一种方法创建一个接口,但是您可以根据需要添加任意数量的

For brevity we'll create an interface with just one method, but you can add as many as you need.

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

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

创建接口的实现

在您的示例中,您正在扩展RemoteObject.我建议您对其进行封装:这将更加灵活.更不用说代码中的连接信息是硬编码的,这要求您每次此信息更改时都重新编译应用程序.

In your example you are extending RemoteObject. I would suggest you encapsulate it: that would be a lot more flexible. Not to mention that in your code the connection information is hardcoded which requires you to recompile your application every time this info changes.

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);
    }

}

您还可以选择创建该接口的另一个实现.最常见的用例是创建一个模拟服务,该服务实际上并没有连接到服务器,而是直接返回虚假数据.如果要在没有服务器连接的情况下测试应用程序,则现在可以用模拟服务存根替换真实的服务存根,因为它们都实现了相同的接口.

You also have the option to create another implementation of the interface. The most common use case consists of creating a mock service that doesn't really connect to the server but returns fake data directly. If you want to test your application without server connection you can now just substitute your real service stub with the mock service stub, since they both implement the same interface.

使用实现

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
}

这篇关于如何在Flash Builder Flex中分离远程对象和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆