One of the nice things about the data designer is that the
.xsd file that declaratively stores all of the relevant definitions gets translated
into either VB or C# code at compile and runtime. This means that you can
easily step through it within the debugger, and always understand “what is it
doing for me under the covers”.
To easily see what code has been generated by the data
designer, just open the “Class View” property window in Visual Web Developer or
VS 2005 (if it isn’t visible select the View->Class View menu item), and
navigate to the respective namespaces and types to see the object model of
them. You can then right-click and choose “Browse Definition” to jump to the
generated code for any method:
Figure 28
In addition to viewing the generated code, you can also add
to it and extend it in many ways. You can do this either by sub-classing the
generated objects, or alternatively by using the new “Partial” keyword feature
now supported by the .NET 2.0 language compilers. The partial keyword enables
you to add any method, property or events you want to the classes generated
within the DAL, and they will be compiled together into a single generated
type. Because the partial class code you write lives in a separate file from
the files generated by the designer, you do not have to worry about the
designer ever clobbering or overwriting your code.
To see a simple example of how you could use this feature, I
could easily add an additional “GetProducts()” method to each row of Suppliers
within my Northwind DAL (allowing me to easily obtain the ProductDataTable for
the specific Supplier I have a reference to). Enabling this is as easy as
adding a “SuppliersRow.vb” file in my app_code directory and adding this code
to it:
Partial Public Class NorthWind
Partial Public Class SuppliersRow
Public Function GetProducts() As ProductsDataTable
Dim productsAdapter As New NorthwindTableAdapters.ProductsTableAdapter
Return productsAdapter.GetProductsBySupplier(Me.SupplierID)
End Function
End Class
End Class
This tells the VB compiler that I want to add a
“GetProducts()” method to the SuppliersRow class (I’m using the “SupplierID”
property on the supplier’s row to automatically figure out the subset of
products by Supplier – note that I can write “Me.SupplierId” – since this
method is part of the SuppliersRow class).
Once I add this I can then write this code to easily
hierarchically list each product produced by each Supplier:
Dim suppliersAdapter As New NorthwindTableAdapters.SuppliersTableAdapter
Dim suppliers As Northwind.SuppliersDataTable
Dim supplier As Northwind.SuppliersRow
suppliers = suppliersAdapter.GetAllSuppliers()
For Each supplier In suppliers
Response.Write("Supplier: " & supplier.CompanyName & "<br>")
Dim products As Northwind.ProductsDataTable
Dim product As Northwind.ProductsRow
products = supplier.GetProducts()
For Each product In products
Response.Write("------- Product: " & product.ProductName & "<br>")
Next
Next
And I could easily build this page using a
<asp:DataList> and <asp:Repeater> control to databind a list of
suppliers, and then allow a user to click any of the suppliers to list a
hierarchical drill-down of their products:
Figure 29
I’d do this by databinding the suppliers against the
DataList:
Hierarchy.aspx.vb:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim suppliersAdapter As New NorthwindTableAdapters.SuppliersTableAdapter
DataList1.DataSource = suppliersAdapter.GetAllSuppliers()
DataList1.DataBind()
End Sub
And then using the “Select” feature of DataList to have the
selected item template hierarchically retrieve and display the products (this
way only the selected supplier’s products are retrieved):
Hierarchy.aspx:
<h1>Supplier/Product Drilldown:</h1>
<p>Click a Supplier to List Its Products</p>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:linkbutton ID="Details" CommandName="Select"
runat="server"><%#Eval("CompanyName") %></asp:linkbutton>
</ItemTemplate>
<SelectedItemTemplate>
<div class="selectedsupplier">
<strong><%#Eval("CompanyName") %></strong>
<ul>
<asp:Repeater ID="ProductsList"
DataSource='<%# Container.DataItem.Row.GetProducts() %>' runat="server">
<ItemTemplate>
<li><%#Eval("ProductName")%></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</SelectedItemTemplate>
</asp:DataList>