Section 9.2. Creating a Custom Data Source


9.2. Creating a Custom Data Source

If you want the full power of data access at your disposal and do not want to stick to the data structure provided by the data source, you can implement your data source by yourself, as a server-side ASP.NET class. Since Atlas relies heavily on web services, you have to implement a DataService class. The associated class is implemented in the Microsoft.Web.Services namespace. Within the DataService class, you have to implement the default methods for a data object: they are listed in the enumeration System.ComponentModel.DataObjectMethodType and include the following:

  • Delete

  • Insert

  • Select

  • Update

9.1.3. Displaying Data from a Custom Data Source

For demonstration purposes, we will first implement a web service SELECT method, which retrieves data fromyou guessed itthe Purchasing.Vendors table in the AdventureWorks database.

You can, as before, implement a method that returns the desired data. By using the [DataObjectMethod(DataObjectMethodType.Select)] attribute, you declare the specific method as the "select" method. The actual naming of the method is arbitrary. As data type, you can again return a custom type, as shown in Example 9-5.

Example 9-5. Returning a custom type

 ListViewVendorsDataServiceCustomType.asmx, excerpt [DataObjectMethod(DataObjectMethodType.Select)] public Vendor[] GetVendors() {   SqlConnection conn = new SqlConnection(     "server=(local)\\SQLEXPRESS; Integrated Security=true; Initial Catalog=AdventureWorks");   conn.Open();   SqlCommand comm = new SqlCommand(     "SELECT TOP 10 AccountNumber, Name FROM Purchasing.Vendor",     conn);   SqlDataReader dr = comm.ExecuteReader();   List<Vendor> v = new List<Vendor>();   while (dr.Read())   {     v.Add(new Vendor(       dr["AccountNumber"].ToString(),       dr["Name"].ToString()));   }   return v.ToArray(); } 

As an alternative, you can also return a DataTable, which requires less code, as shown in Example 9-6.

Example 9-6. Returning a DataTable

 ListViewVendorsDataService.asmx <%@ WebService Language="C#"  %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Data; using System.Data.SqlClient; using Microsoft.Web.Services; using System.ComponentModel; [WebService(Namespace = "http://hauser-wenz.de/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class VendorsDataService : DataService { [DataObjectMethod(DataObjectMethodType.Select)]   public DataTable GetVendors()   {     SqlConnection conn = new SqlConnection(       "server=(local)\\SQLEXPRESS; Integrated Security=true; Initial Catalog=AdventureWorks");     conn.Open();     SqlCommand comm = new SqlCommand(       "SELECT TOP 10 AccountNumber, Name FROM Purchasing.Vendor",       conn);     SqlDataAdapter adap = new SqlDataAdapter(comm);     DataSet ds = new DataSet();     adap.Fill(ds); return ds.Tables[0];   } } 

These .asmx files, so far, do not contain something labeled with [WebMethod]. However, when you call one of these web services in the browser directly, you see that they have two of them: Getdata() and SaveData(). Both expect a parameters array for fine-tuning. Atlas offers automated support for this, so you are just calling the methods under the fixed DataObjectMethodType names: Delete, Insert, Select, and Update. The results are shown in Figure 9-3.

Figure 9-3. The methods provided by the base class


Example 9-6 comes in two compatible flavors: DataTable and custom type. You can find in the code downloads for this book under the filenames ListViewVendorsDataService.asmx and ListViewVendorsDataServiceCustomType.asmx. The custom type has .txt appended to its filename to avoid data type clashes with ListViewVendorsCustomType.asmx.


On the ASP.NET side of things, two items are required: HTML markup to define the output template and xml-script markup to do the data binding. The former is the same as before: an HTML table. Remember to use <thead> and <tbody>, to satisfy Internet Explorer. The following HTML markup serves as the placeholder to which Atlas binds the data from the custom data source:

 <div >   vendor list goes here</div> <div style="display: none;">   <div >     <table>     <thead>         <tr><th>Account Number</th><th>Name</th></tr>     </thead>       <tbody>         <tr >           <td >vendor account number goes here</td>           <td >vendor name goes here</td>         </tr>     </tbody>     </table>   </div> </div> 

The xml-script part however requires some changes from the preceding example. It starts off as usual:

 <script type="text/xml-script">   <page xmlns="http://schemas.microsoft.com/xml-script/2005">     <components>     ...     </components>   </page> </script> 

Then, the data source needs to be referenced. Since this is no ordinary web service, the ScriptManager object will not work to reference the web service. Instead, the <dataSource> xml-script element comes into play. Provide the URL and an IDyou will need the latter later on!

 <script type="text/xml-script">   <page xmlns="http://schemas.microsoft.com/xml-script/2005">     <components>     <dataSource  serviceURL="ListViewVendorsDataService.asmx" />       ...     </components>   </page> </script> 

Next up is the ListView control; therefore, the <listView> element enters the stage. The most important step is to bind the data source from the preceding code snippet to the ListView control. The properties dataPath and property must be set to data, and dataContext must reference the ID of the <dataSource> element:

 <listView  itemTemplateParentElement targetElement="output">   <bindings>     <binding dataContext="vendorSource" dataPath="data" property="data" />   </bindings>   ... </listView> 

The <layoutTemplate> and <itemTemplate> elements are the same as before, binding the data to the <table> element and its subelements.

One thing is missing, however. The data is bound, but has not been loaded yet. The data source supports the property autoLoad. If set to "TRue", this automatically calls the Select method of the data source.

See Example 9-7for the complete code for this task.

Example 9-7. Displaying data from a custom data source

 ListViewDataService.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">   <title>Atlas</title> </head> <body>   <form  runat="server">     <atlas:ScriptManager runat="server">     </atlas:ScriptManager>     <div >       vendor list goes here</div>     <div style="display: none;">       <div >         <table>           <thead>             <tr><th>Account Number</th><th>Name</th></tr>           </thead>           <tbody >             <tr >               <td >vendor account number goes here</td>               <td >vendor name goes here</td>             </tr>           </tbody>         </table>       </div>     </div>   </form>   <script type="text/xml-script">     <page xmlns="http://schemas.microsoft.com/xml-script/2005">       <components>         <dataSource  serviceURL="ListViewVendorsDataService.asmx" autoLoad="true" />         <listView  itemTemplateParentElement >           <bindings>             <binding dataContext="vendorSource" dataPath="data" property="data" />           </bindings>           <layoutTemplate>             <template layoutElement="vendorsLayout" />           </layoutTemplate>           <itemTemplate>             <template layoutElement="vendorsItem">               <label >                 <bindings>                   <binding dataPath="AccountNumber" property="text" />                 </bindings>               </label>               <label >                 <bindings>                   <binding dataPath="Name" property="text" />                 </bindings>               </label>             </template>           </itemTemplate>         </listView>       </components>     </page>   </script> </body> </html> 

So there is no actual coding (except of the DataService web service) involved, just declarations. The output shows the first 10 elements in the Purchasing.Vendors table, formatted in an HTML <table> element. Therefore, the output of this script is identical to the one in Figure 9-2.

Managing Data

Displaying data is just the first step. The logical consequence would be to implement the other methods defined in System.ComponentModel.DataObjectMethodType. Then you can page through the data, creating a whole set of new possibilities. However in this specific case, you would get better performance and develop more efficiently using the ASP.NET GridView control (or any other suitable data control). If you are concerned about the postbacks and page refreshes that are fundamental to this control, have a look at Chapter 1 where you will learn of a way to overcome this limitationwith heavy use of Atlas, of course.





Programming Atlas
Programming Atlas
ISBN: 0596526725
EAN: 2147483647
Year: 2006
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net