A Day in the Life of a Developer
Use Strong-typed Collections in N-Tier Apps
Eliminate System.Data classes from the presentation
layer.
By Doug Seven
In our current client project, we wanted to provide clean n-tier application design, eliminating
any System.Data classes from the
presentation layer. We also wanted to make sure the solution was flexible
enough for use in Web applications use Web Services. We basically wanted to
build an n-tier base class library we
could build our client applications on top of. We accomplished this by building
classes to represent our data and strong-typed collections to represent
multiple instances of these classes.
The classes that represent the data are quite basic - they
expose properties to represent the data, and that's about it. If you look at
the ProductProduct.cs class in the
sample code, you'll see we created a set of public properties to represent
various fields of the Product table
in the Northwind database. In the class definition, we added the <Serializable> attribute, which
enables an instance of this class to be serialized to XML for use in a Web
service:
<Serializable()> Public Class Product
The real key to this solution was in creating the
strong-typed collections - a collection of objects with the same class
definition. One of the primary reasons for doing this was to eliminate the use
of DataSets wherever possible and
instead use strong-typed collections. Although a collection doesn't have all
the functionality of a DataSet, it
does provide the ability to contain multiple instances of the same class and
can be data-bound to Web controls, such as a DataGrid. The DataSet is
a rather large object (large memory footprint), while a collection has a much
smaller memory footprint.
A strong-typed collection is a class (ProductCollectionProductCollection.cs) that derives from the System.Collections.CollectionBase
class. This enables us to provide interfaces to the methods of the CollectionBase.List object, which is
where the objects are held in the collection. When we create the interfaces for
the CollectionBase.List property, we
can specify the type of object that can be put into or pulled out of the
collection:
<Serializable()>
_
Public Class
ProductCollection
Inherits
System.Collections.CollectionBase
'==============================================================
' Item Property - public - ReadOnly
'--------------------------------------------------------------
' Returns the Product with the specified
Index
'==============================================================
Default Public
ReadOnly Property Item(ByVal Index As Int32) _
As
Components.Product
Get
Return
Me.List.Item(Index)
End Get
End Property
'==============================================================
' Add() Method
'--------------------------------------------------------------
' Adds the specified Product object to the
CollectionBase.List
'==============================================================
Public Sub
Add(ByVal Product As Components.Product)
Me.List.Add(Product)
End Sub
End Class
Next, we created a data-access class, ProductDB.cs, which acts as the go-between for the application and
the database. The data class uses SqlDataReader
to get the data. The data reader is passed into a CreateProduct helper method, where an instance of the Product class is created and returned
to either be returned to the caller or added to a ProductCollection object:
Dim _reader As
SqlDataReader = _cmd.ExecuteReader()
_reader.Read()
'Create the Product
object
_product =
makeProduct(_reader)
'Close the SqlDataReader
when done
_reader.Close()
In the end, we have a Product
object or a ProductCollection object
populated with instances of Product
objects. Either of these objects can be returned from a Web service because
they both implement the <Serializable>
attribute (see the Service1.asmx class). Additionally, you can bind the ProductCollection object to any data
control (see the WebForm1.aspx Web form).
The files referenced in this article are available for
download.
Doug Seven is a co-founder of http://www.dotnetjunkies, a content-based
online training resource for .NET developers. He has been building applications
with the .NET Framework since the summer of 2000 and has co-authored five books
related to the .NET Framework: Programming
Data-Driven Web Applications with ASP.NET (Sams), ASP.NET: Tips, Tutorials & Code (Sams), Professional ADO.NET (Wrox), Developing Custom Controls for ASP.NET (Sams),
and Professional ASP.NET Security (Wrox).