上一篇:Flex与.NET互操作(七):了解FluorineFx的环境配置(远程对象、网关、通道、目的地)

关于远程访问在本系列文章中陆续的写了不少示例了,本文没有准备深入的去探讨,为了巩固FluorineFx网关的学习和使用。于此,本文将使用FluorineFx网关来提供数据服务等多项功能来介绍通过FluorineFx实现远程访问的相关知识点。

    FluorineFx提供的远程访问包括有很多方面的知道点,本文只介绍其中的三个知识点:访问远程对象返回对象,返回DataTable,返回DataSet对象.FluorineFx安装包里自带有相关的示例程序,要学习更多可直接参考这些示例程序.

    在实现访问前我们同样来做一些准备工作,建立好远程对象,如下:
  1. 1 namespace Fluorine.ServiceLibrary
  2. 2 {
  3. 3    public class Book
  4. 4    {
  5. 5        public int Id { get; set; }
  6. 6        public string Name { get; set; }
  7. 7        public string Author { get; set; }
  8. 8        public double Price { get; set; }
  9. 9    }
  10. 10 }
复制代码
下面是提供Flex访问的远程对象:
  1. 1 namespace Fluorine.ServiceLibrary
  2. 2 {
  3. 3    [RemotingService]
  4. 4    public class FluorineService
  5. 5    {
  6. 6        /// <summary>
  7. 7        /// 返回一个简单对象
  8. 8        /// </summary>
  9. 9        /// <returns></returns>
  10. 10        public Book GetBook()
  11. 11        {
  12. 12            return new Book
  13. 13            {
  14. 14                Id = 1,
  15. 15                Name = "《三国演义》",
  16. 16                Author = "罗贯中",
  17. 17                Price = 100
  18. 18            };
  19. 19        }
  20. 20
  21. 21        /// <summary>
  22. 22        /// 返回DataTable对象
  23. 23        /// </summary>
  24. 24        /// <returns></returns>
  25. 25        [DataTableType("Fluorine.ServiceLibrary.Book")]
  26. 26        public DataTable GetDataTable()
  27. 27        {
  28. 28            DataTable dt = new DataTable("Book");
  29. 29            dt.Columns.Add("Id", typeof(int));
  30. 30            dt.Columns.Add("Name", typeof(string));
  31. 31            dt.Columns.Add("Author", typeof(string));
  32. 32            dt.Columns.Add("Price", typeof(double));
  33. 33
  34. 34            DataRow dr = dt.NewRow();
  35. 35            dr["Id"] = 1;
  36. 36            dr["Name"] = "《三国演义》";
  37. 37            dr["Author"] = "罗贯中";
  38. 38            dr["Price"] = 52.30;
  39. 39            dt.Rows.Add(dr);
  40. 40
  41. 41            dr = dt.NewRow();
  42. 42            dr["Id"] = 2;
  43. 43            dr["Name"] = "《西游记》";
  44. 44            dr["Author"] = "吴承恩";
  45. 45            dr["Price"] = 39.91;
  46. 46            dt.Rows.Add(dr);
  47. 47
  48. 48            return dt;
  49. 49        }
  50. 50
  51. 51        /// <summary>
  52. 52        /// 返回DataSet对象
  53. 53        /// </summary>
  54. 54        /// <returns></returns>
  55. 55        [DataSetType("Fluorine.ServiceLibrary.Book")]
  56. 56        public DataSet GetDataSet()
  57. 57        {
  58. 58            DataSet ds = new DataSet("DS");
  59. 59            DataTable dt = ds.Tables.Add("Books");
  60. 60            dt.Columns.Add("Id", typeof(int));
  61. 61            dt.Columns.Add("Name", typeof(string));
  62. 62            dt.Columns.Add("Author", typeof(string));
  63. 63            dt.Columns.Add("Price", typeof(double));
  64. 64
  65. 65            DataRow dr = dt.NewRow();
  66. 66            dr["Id"] = 1;
  67. 67            dr["Name"] = "《三国演义》";
  68. 68            dr["Author"] = "罗贯中";
  69. 69            dr["Price"] = 52.30;
  70. 70            dt.Rows.Add(dr);
  71. 71
  72. 72            dr = dt.NewRow();
  73. 73            dr["Id"] = 2;
  74. 74            dr["Name"] = "《西游记》";
  75. 75            dr["Author"] = "吴承恩";
  76. 76            dr["Price"] = 39.91;
  77. 77            dt.Rows.Add(dr);
  78. 78
  79. 79            return ds;
  80. 80        }
  81. 81
  82. 82    }
  83. 83 }
复制代码
上面代码片段中分别提供了返回一个对象,DataTable,DataSet对象的方法。这里只需要记住两个关键标识就行,它门是:DataTableType和DataSetType.  下面通过Flex的非可视化组件<mx:RemoteObject>来访问远程对象,OK,下面我们来看看具体怎么来调用。
  1. 1 <mx:RemoteObject id="ro" destination="fluorine" >
  2. 2    source="Fluorine.ServiceLibrary.FluorineService"
  3. 3    fault="onFaultHandler(event)"
  4. 4    <mx:method name="GetBook" result="onGetBookHandler(event)"/>
  5. 5    <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
  6. 6    <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
  7. 7 </mx:RemoteObject>
复制代码
一、返回对象示例
  1. 1 [Binable]
  2. 2 private var book:BookVO;
  3. 3   
  4. 4 private function onGetBookHandler(evt:ResultEvent):void
  5. 5 {
  6. 6    book=evt.result as BookVO;
  7. 7 }
复制代码
通过点击按扭调用远程方法GetBook()完成方法的调用,直接可以将返回结果绑定到界面元素上。
  1. 1 <mx:Button label="Book" click="ro.GetBook()"/>
  2. 2 <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
复制代码
二、返回DataTable对象

    返回DataTable和DataSet,将结果绑定到DataGrid上显示,先看看DataGrid的定义:
  1. 1 <mx:DataGrid x="10" y="10" width="543" height="147" dataProvider="{books}">
  2. 2    <mx:columns>
  3. 3            <mx:DataGridColumn headerText="编号" dataField="Id"/>
  4. 4            <mx:DataGridColumn headerText="书名" dataField="Name"/>
  5. 5            <mx:DataGridColumn headerText="作者" dataField="Author"/>
  6. 6            <mx:DataGridColumn headerText="价格" dataField="Price"/>
  7. 7    </mx:columns>
  8. 8 </mx:DataGrid>
复制代码
DataGrid的数据源为定义的一个ArrayCollection对象,详细如下:
  1. 1 [Binable]
  2. 2 private var books:ArrayCollection;


  3. 1 private function onGetDataTableHandler(evt:ResultEvent):void
  4. 2 {
  5. 3    books=evt.result as ArrayCollection;
  6. 4 }
复制代码
三、返回DataTable对象
  1. 1 private function onGetDataSetHandler(evt:ResultEvent):void
  2. 2 {
  3. 3    books=evt.result as ArrayCollection;
  4. 4 }
复制代码
如上便完成了通过FluorineFx网关来实现远程访问,下面是完整的Flex端代码,实现很简单这里就不作详细讲解:
  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3. 3    <mx:Script>
  4. 4        <![CDATA[
  5. 5            import mx.controls.Alert;
  6. 6            import mx.rpc.events.ResultEvent;
  7. 7            import mx.rpc.events.FaultEvent;
  8. 8            import mx.collections.ArrayCollection;
  9. 9            [Binable]
  10. 10            private var books:ArrayCollection;
  11. 11            [Binable]
  12. 12            private var book:BookVO;
  13. 13           
  14. 14            private function onGetBookHandler(evt:ResultEvent):void
  15. 15            {
  16. 16                book=evt.result as BookVO;
  17. 17            }
  18. 18           
  19. 19            private function onGetDataTableHandler(evt:ResultEvent):void
  20. 20            {
  21. 21                books=evt.result as ArrayCollection;
  22. 22            }
  23. 23           
  24. 24            private function onGetDataSetHandler(evt:ResultEvent):void
  25. 25            {
  26. 26                books=evt.result as ArrayCollection;
  27. 27            }
  28. 28           
  29. 29            private function onFaultHandler(evt:FaultEvent):void
  30. 30            {
  31. 31                Alert.show(evt.fault.faultDetail);
  32. 32            }
  33. 33        ]]>
  34. 34    </mx:Script>
  35. 35    <mx:Panel x="42" y="56" width="578" height="226" layout="absolute" fontSize="12">
  36. 36    <mx:DataGrid x="10" y="10" width="543" height="147" dataProvider="{books}">
  37. 37        <mx:columns>
  38. 38                <mx:DataGridColumn headerText="编号" dataField="Id"/>
  39. 39                <mx:DataGridColumn headerText="书名" dataField="Name"/>
  40. 40                <mx:DataGridColumn headerText="作者" dataField="Author"/>
  41. 41                <mx:DataGridColumn headerText="价格" dataField="Price"/>
  42. 42        </mx:columns>
  43. 43    </mx:DataGrid>
  44. 44    <mx:ControlBar>
  45. 45        <mx:Button label="DataTable" click="getDataTable()"/>
  46. 46        <mx:Button label="DataSet" click="getDataSet()"/>
  47. 47        <mx:Button label="Book" click="ro.GetBook()"/>
  48. 48        <mx:TextInput width="302" text="{boo.Name+book.Author+book.Price}"/>
  49. 49    </mx:ControlBar>
  50. 50    </mx:Panel>
  51. 51    <mx:RemoteObject id="ro" destination="fluorine" >
  52. 52        source="Fluorine.ServiceLibrary.FluorineService"
  53. 53        fault="onFaultHandler(event)"
  54. 54        <mx:method name="GetBook" result="onGetBookHandler(event)"/>
  55. 55        <mx:method name="GetDataTable" result="onGetDataTableHandler(event)"/>
  56. 56        <mx:method name="GetDataSet" result="onGetDataSetHandler(event)"/>
  57. 57    </mx:RemoteObject>
  58. 58 </mx:Application>
  59. 59
复制代码
(文/Bēniaǒ

下一篇:Flex与.NET互操作(九):FluorineFx.NET的认证与授权
TOP