Flex与.NET互操作(八):使用FluorineFx网关实现远程访问
关于远程访问在本系列文章中陆续的写了不少示例了,本文没有准备深入的去探讨,为了巩固FluorineFx网关的学习和使用。于此,本文将使用FluorineFx网关来提供数据服务等多项功能来介绍通过FluorineFx实现远程访问的相关知识点。
FluorineFx提供的远程访问包括有很多方面的知道点,本文只介绍其中的三个知识点:访问远程对象返回对象,返回DataTable,返回DataSet对象.FluorineFx安装包里自带有相关的示例程序,要学习更多可直接参考这些示例程序.
在实现访问前我们同样来做一些准备工作,建立好远程对象,如下:
1 namespace Fluorine.ServiceLibrary
2 {
3 public class Book
4 {
5 public int Id { get; set; }
6 public string Name { get; set; }
7 public string Author { get; set; }
8 public double Price { get; set; }
9 }
10 }
2 {
3 public class Book
4 {
5 public int Id { get; set; }
6 public string Name { get; set; }
7 public string Author { get; set; }
8 public double Price { get; set; }
9 }
10 }
下面是提供Flex访问的远程对象:
Code
上面代码片段中分别提供了返回一个对象,DataTable,DataSet对象的方法。这里只需要记住两个关键标识就行,它门是:DataTableType和DataSetType. 下面通过Flex的非可视化组件<mx:RemoteObject>来访问远程对象,OK,下面我们来看看具体怎么来调用。
1 <mx:RemoteObject id="ro" destination="fluorine" >
2 source="Fluorine.ServiceLibrary.FluorineService"
3 fault="onFaultHandler(event)"
4 <mx:method name="GetBook" result="onGetBookHandler(event)"/>
5 <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
6 <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
7 </mx:RemoteObject>
2 source="Fluorine.ServiceLibrary.FluorineService"
3 fault="onFaultHandler(event)"
4 <mx:method name="GetBook" result="onGetBookHandler(event)"/>
5 <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
6 <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
7 </mx:RemoteObject>
一、返回对象示例
1 [Binable]
2 private var book:BookVO;
3
4 private function onGetBookHandler(evt:ResultEvent):void
5 {
6 book=evt.result as BookVO;
7 }
2 private var book:BookVO;
3
4 private function onGetBookHandler(evt:ResultEvent):void
5 {
6 book=evt.result as BookVO;
7 }
通过点击按扭调用远程方法GetBook()完成方法的调用,直接可以将返回结果绑定到界面元素上。
1 <mx:Button label="Book" click="ro.GetBook()"/>
2 <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
2 <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
二、返回DataTable对象
返回DataTable和DataSet,将结果绑定到DataGrid上显示,先看看DataGrid的定义:
1 <mx:DataGrid x="10" y="10" width="543" height="147" dataProvider="{books}">
2 <mx:columns>
3 <mx:DataGridColumn headerText="编号" dataField="Id"/>
4 <mx:DataGridColumn headerText="书名" dataField="Name"/>
5 <mx:DataGridColumn headerText="作者" dataField="Author"/>
6 <mx:DataGridColumn headerText="价格" dataField="Price"/>
7 </mx:columns>
8 </mx:DataGrid>
2 <mx:columns>
3 <mx:DataGridColumn headerText="编号" dataField="Id"/>
4 <mx:DataGridColumn headerText="书名" dataField="Name"/>
5 <mx:DataGridColumn headerText="作者" dataField="Author"/>
6 <mx:DataGridColumn headerText="价格" dataField="Price"/>
7 </mx:columns>
8 </mx:DataGrid>
DataGrid的数据源为定义的一个ArrayCollection对象,详细如下:
1 [Binable]
2 private var books:ArrayCollection;
2 private var books:ArrayCollection;
1 private function onGetDataTableHandler(evt:ResultEvent):void
2 {
3 books=evt.result as ArrayCollection;
4 }
2 {
3 books=evt.result as ArrayCollection;
4 }
三、返回DataTable对象
1 private function onGetDataSetHandler(evt:ResultEvent):void
2 {
3 books=evt.result as ArrayCollection;
4 }
2 {
3 books=evt.result as ArrayCollection;
4 }
如上便完成了通过FluorineFx网关来实现远程访问,下面是完整的Flex端代码,实现很简单这里就不作详细讲解:
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3 <mx:Script>
4 <![CDATA[
5 import mx.controls.Alert;
6 import mx.rpc.events.ResultEvent;
7 import mx.rpc.events.FaultEvent;
8 import mx.collections.ArrayCollection;
9 [Bindable]
10 private var books:ArrayCollection;
11 [Bindable]
12 private var book:BookVO;
13
14 private function onGetBookHandler(evt:ResultEvent):void
15 {
16 book=evt.result as BookVO;
17 }
18
19 private function onGetDataTableHandler(evt:ResultEvent):void
20 {
21 books=evt.result as ArrayCollection;
22 }
23
24 private function onGetDataSetHandler(evt:ResultEvent):void
25 {
26 books=evt.result as ArrayCollection;
27 }
28
29 private function onFaultHandler(evt:FaultEvent):void
30 {
31 Alert.show(evt.fault.faultDetail);
32 }
33 ]]>
34 </mx:Script>
35 <mx:Panel x="42" y="56" width="578" height="226" layout="absolute" fontSize="12">
36 <mx:DataGrid x="10" y="10" width="543" height="147" dataProvider="{books}">
37 <mx:columns>
38 <mx:DataGridColumn headerText="编号" dataField="Id"/>
39 <mx:DataGridColumn headerText="书名" dataField="Name"/>
40 <mx:DataGridColumn headerText="作者" dataField="Author"/>
41 <mx:DataGridColumn headerText="价格" dataField="Price"/>
42 </mx:columns>
43 </mx:DataGrid>
44 <mx:ControlBar>
45 <mx:Button label="DataTable" click="getDataTable()"/>
46 <mx:Button label="DataSet" click="getDataSet()"/>
47 <mx:Button label="Book" click="ro.GetBook()"/>
48 <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
49 </mx:ControlBar>
50 </mx:Panel>
51 <mx:RemoteObject id="ro" destination="fluorine" >
52 source="Fluorine.ServiceLibrary.FluorineService"
53 fault="onFaultHandler(event)"
54 <mx:method name="GetBook" result="onGetBookHandler(event)"/>
55 <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
56 <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
57 </mx:RemoteObject>
58 </mx:Application>
59
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3 <mx:Script>
4 <![CDATA[
5 import mx.controls.Alert;
6 import mx.rpc.events.ResultEvent;
7 import mx.rpc.events.FaultEvent;
8 import mx.collections.ArrayCollection;
9 [Bindable]
10 private var books:ArrayCollection;
11 [Bindable]
12 private var book:BookVO;
13
14 private function onGetBookHandler(evt:ResultEvent):void
15 {
16 book=evt.result as BookVO;
17 }
18
19 private function onGetDataTableHandler(evt:ResultEvent):void
20 {
21 books=evt.result as ArrayCollection;
22 }
23
24 private function onGetDataSetHandler(evt:ResultEvent):void
25 {
26 books=evt.result as ArrayCollection;
27 }
28
29 private function onFaultHandler(evt:FaultEvent):void
30 {
31 Alert.show(evt.fault.faultDetail);
32 }
33 ]]>
34 </mx:Script>
35 <mx:Panel x="42" y="56" width="578" height="226" layout="absolute" fontSize="12">
36 <mx:DataGrid x="10" y="10" width="543" height="147" dataProvider="{books}">
37 <mx:columns>
38 <mx:DataGridColumn headerText="编号" dataField="Id"/>
39 <mx:DataGridColumn headerText="书名" dataField="Name"/>
40 <mx:DataGridColumn headerText="作者" dataField="Author"/>
41 <mx:DataGridColumn headerText="价格" dataField="Price"/>
42 </mx:columns>
43 </mx:DataGrid>
44 <mx:ControlBar>
45 <mx:Button label="DataTable" click="getDataTable()"/>
46 <mx:Button label="DataSet" click="getDataSet()"/>
47 <mx:Button label="Book" click="ro.GetBook()"/>
48 <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
49 </mx:ControlBar>
50 </mx:Panel>
51 <mx:RemoteObject id="ro" destination="fluorine" >
52 source="Fluorine.ServiceLibrary.FluorineService"
53 fault="onFaultHandler(event)"
54 <mx:method name="GetBook" result="onGetBookHandler(event)"/>
55 <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
56 <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
57 </mx:RemoteObject>
58 </mx:Application>
59