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

查看:22
本文介绍了如何在 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天全站免登陆