关于远程访问在本系列文章中陆续的写了不少示例了,本文没有准备深入的去探讨,为了巩固FluorineFx网关的学习和使用。于此,本文将使用FluorineFx网关来提供数据服务等多项功能来介绍通过FluorineFx实现远程访问的相关知识点。
FluorineFx提供的远程访问包括有很多方面的知道点,本文只介绍其中的三个知识点:访问远程对象返回对象,返回DataTable,返回DataSet对象.FluorineFx安装包里自带有相关的示例程序,要学习更多可直接参考这些示例程序.
在实现访问前我们同样来做一些准备工作,建立好远程对象,如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1namespaceFluorine.ServiceLibrary
2{
3publicclassBook
4{
5publicintId{get;set;}
6publicstringName{get;set;}
7publicstringAuthor{get;set;}
8publicdoublePrice{get;set;}
9}
10}
下面是提供Flex访问的远程对象:
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1namespaceFluorine.ServiceLibrary
2{
3[RemotingService]
4publicclassFluorineService
5{
6///<summary>
7///返回一个简单对象
8///</summary>
9///<returns></returns>
10publicBookGetBook()
11{
12returnnewBook
13{
14Id=1,
15Name="《三国演义》",
16Author="罗贯中",
17Price=100
18};
19}
20
21///<summary>
22///返回DataTable对象
23///</summary>
24///<returns></returns>
25[DataTableType("Fluorine.ServiceLibrary.Book")]
26publicDataTableGetDataTable()
27{
28DataTabledt=newDataTable("Book");
29dt.Columns.Add("Id",typeof(int));
30dt.Columns.Add("Name",typeof(string));
31dt.Columns.Add("Author",typeof(string));
32dt.Columns.Add("Price",typeof(double));
33
34DataRowdr=dt.NewRow();
35dr["Id"]=1;
36dr["Name"]="《三国演义》";
37dr["Author"]="罗贯中";
38dr["Price"]=52.30;
39dt.Rows.Add(dr);
40
41dr=dt.NewRow();
42dr["Id"]=2;
43dr["Name"]="《西游记》";
44dr["Author"]="吴承恩";
45dr["Price"]=39.91;
46dt.Rows.Add(dr);
47
48returndt;
49}
50
51///<summary>
52///返回DataSet对象
53///</summary>
54///<returns></returns>
55[DataSetType("Fluorine.ServiceLibrary.Book")]
56publicDataSetGetDataSet()
57{
58DataSetds=newDataSet("DS");
59DataTabledt=ds.Tables.Add("Books");
60dt.Columns.Add("Id",typeof(int));
61dt.Columns.Add("Name",typeof(string));
62dt.Columns.Add("Author",typeof(string));
63dt.Columns.Add("Price",typeof(double));
64
65DataRowdr=dt.NewRow();
66dr["Id"]=1;
67dr["Name"]="《三国演义》";
68dr["Author"]="罗贯中";
69dr["Price"]=52.30;
70dt.Rows.Add(dr);
71
72dr=dt.NewRow();
73dr["Id"]=2;
74dr["Name"]="《西游记》";
75dr["Author"]="吴承恩";
76dr["Price"]=39.91;
77dt.Rows.Add(dr);
78
79returnds;
80}
81
82}
83}
上面代码片段中分别提供了返回一个对象,DataTable,DataSet对象的方法。这里只需要记住两个关键标识就行,它门是:DataTableType和DataSetType. 下面通过Flex的非可视化组件<mx:RemoteObject>来访问远程对象,OK,下面我们来看看具体怎么来调用。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
<
mx:RemoteObjectid
=
"
ro
"
destination
=
"
fluorine
"
>
2
source
=
"
Fluorine.ServiceLibrary.FluorineService
"
3
fault
=
"
onFaultHandler(event)
"
4
<
mx:methodname
=
"
GetBook
"
result
=
"
onGetBookHandler(event)
"
/>
5
<
mx:methodname
=
"
GetDataTable
"
result
=
"
onGetDataTableHandler(event)
"
/>
6
<
mx:methodname
=
"
GetDataSet
"
result
=
"
onGetDataSetHandler(event)
"
/>
7
</
mx:RemoteObject
>
一、返回对象示例
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
[Binable]
2
private
varbook:BookVO;
3
4
private
functiononGetBookHandler(evt:ResultEvent):void
5
{
6
book
=
evt.result
as
BookVO;
7
}
通过点击按扭调用远程方法GetBook()完成方法的调用,直接可以将返回结果绑定到界面元素上。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
<
mx:Buttonlabel
=
"
Book
"
click
=
"
ro.GetBook()
"
/>
2
<
mx:TextInputwidth
=
"
302
"
text
=
"
{boo.Name+book.Author+book.Price}
"
/>
二、返回DataTable对象
返回DataTable和DataSet,将结果绑定到DataGrid上显示,先看看DataGrid的定义:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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
>
DataGrid的数据源为定义的一个ArrayCollection对象,详细如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
[Binable]
2
privatevarbooks:ArrayCollection;
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
private
functiononGetDataTableHandler(evt:ResultEvent):void
2
{
3
books
=
evt.result
as
ArrayCollection;
4
}
三、返回DataTable对象
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
private
functiononGetDataSetHandler(evt:ResultEvent):void
2
{
3
books
=
evt.result
as
ArrayCollection;
4
}
如上便完成了通过FluorineFx网关来实现远程访问,下面是完整的Flex端代码,实现很简单这里就不作详细讲解:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
1
<?
xmlversion="1.0"encoding="utf-8"
?>
2
<
mx:Application
xmlns:mx
="http://www.adobe.com/2006/mxml"
layout
="absolute"
>
3
<
mx:Script
>
4
<!
[CDATA[
5
importmx.controls.Alert;
6
importmx.rpc.events.ResultEvent;
7
importmx.rpc.events.FaultEvent;
8
importmx.collections.ArrayCollection;
9
[Binable]
10
privatevarbooks:ArrayCollection;
11
[Binable]
12
privatevarbook:BookVO;
13
14
privatefunctiononGetBookHandler(evt:ResultEvent):void
15
{
16
book=evt.resultasBookVO;
17
}
18
19
privatefunctiononGetDataTableHandler(evt:ResultEvent):void
20
{
21
books=evt.resultasArrayCollection;
22
}
23
24
privatefunctiononGetDataSetHandler(evt:ResultEvent):void
25
{
26
books=evt.resultasArrayCollection;
27
}
28
29
privatefunctiononFaultHandler(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