Flex与.NET互操作(八) 使用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 }
下面是提供Flex访问的远程对象:
Code
1 namespace Fluorine.ServiceLibrary
2 {
3 [RemotingService]
4 public class FluorineService
5 {
6 /// <summary>
7 /// 返回一个简单对象
8 /// </summary>
9 /// <returns></returns>
10 public Book GetBook()
11 {
12 return new Book
13 {
14 Id = 1,
15 Name = "《三国演义》",
16 Author = "罗贯中",
17 Price = 100
18 };
19 }
20
21 /// <summary>
22 /// 返回DataTable对象
23 /// </summary>
24 /// <returns></returns>
25 [DataTableType("Fluorine.ServiceLibrary.Book")]
26 public DataTable GetDataTable()
27 {
28 DataTable dt = new DataTable("Book");
29 dt.Columns.Add("Id", typeof(int));
30 dt.Columns.Add("Name", typeof(string));
31 dt.Columns.Add("Author", typeof(string));
32 dt.Columns.Add("Price", typeof(double));
33
34 DataRow dr = dt.NewRow();
35 dr["Id"] = 1;
36 dr["Name"] = "《三国演义》";
37 dr["Author"] = "罗贯中";
38 dr["Price"] = 52.30;
39 dt.Rows.Add(dr);
40
41 dr = dt.NewRow();
42 dr["Id"] = 2;
43 dr["Name"] = "《西游记》";
44 dr["Author"] = "吴承恩";
45 dr["Price"] = 39.91;
46 dt.Rows.Add(dr);
47
48 return dt;
49 }
50
51 /// <summary>
52 /// 返回DataSet对象
53 /// </summary>
54 /// <returns></returns>
55 [DataSetType("Fluorine.ServiceLibrary.Book")]
56 public DataSet GetDataSet()
57 {
58 DataSet ds = new DataSet("DS");
59 DataTable dt = ds.Tables.Add("Books");
60 dt.Columns.Add("Id", typeof(int));
61 dt.Columns.Add("Name", typeof(string));
62 dt.Columns.Add("Author", typeof(string));
63 dt.Columns.Add("Price", typeof(double));
64
65 DataRow dr = dt.NewRow();
66 dr["Id"] = 1;
67 dr["Name"] = "《三国演义》";
68 dr["Author"] = "罗贯中";
69 dr["Price"] = 52.30;
70 dt.Rows.Add(dr);
71
72 dr = dt.NewRow();
73 dr["Id"] = 2;
74 dr["Name"] = "《西游记》";
75 dr["Author"] = "吴承恩";
76 dr["Price"] = 39.91;
77 dt.Rows.Add(dr);
78
79 return ds;
80 }
81
82 }
83 }
上面代码片段中分别提供了返回一个对象,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>
一、返回对象示例
1 [Binable]
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}"/>
二、返回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>
DataGrid的数据源为定义的一个ArrayCollection对象,详细如下:
1 [Binable]
2 private var books:ArrayCollection;
1 private function onGetDataTableHandler(evt:ResultEvent):void
2 {
3 books=evt.result as ArrayCollection;
4 }
三、返回DataTable对象
1 private function onGetDataSetHandler(evt:ResultEvent):void
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 [Binable]
10 private var books:ArrayCollection;
11 [Binable]
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>
="COLOR: rgb(0,128,128)">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>