A Day in the Life of a Developer
Use Strong-typed Collections in N-Tier Apps
Eliminate System.Data classes from the presentationlayer.
By Doug Seven
In our current client project, we wanted to provide clean n-tier application design, eliminatingany System.Data classes from thepresentation layer. We also wanted to make sure the solution was flexibleenough for use in Web applications use Web Services. We basically wanted tobuild an n-tier base class library wecould build our client applications on top of. We accomplished this by buildingclasses to represent our data and strong-typed collections to representmultiple instances of these classes.
The classes that represent the data are quite basic - theyexpose properties to represent the data, and that's about it. If you look atthe ProductProduct.cs class in thesample code, you'll see we created a set of public properties to representvarious fields of the Product tablein the Northwind database. In the class definition, we added the <Serializable> attribute, whichenables an instance of this class to be serialized to XML for use in a Webservice:
<Serializable()> Public Class Product
The real key to this solution was in creating thestrong-typed collections - a collection of objects with the same classdefinition. One of the primary reasons for doing this was to eliminate the useof DataSets wherever possible andinstead use strong-typed collections. Although a collection doesn't have allthe functionality of a DataSet, itdoes provide the ability to contain multiple instances of the same class andcan be data-bound to Web controls, such as a DataGrid. The DataSet isa rather large object (large memory footprint), while a collection has a muchsmaller memory footprint.
A strong-typed collection is a class (ProductCollectionProductCollection.cs) that derives from the System.Collections.CollectionBaseclass. This enables us to provide interfaces to the methods of the CollectionBase.List object, which iswhere the objects are held in the collection. When we create the interfaces forthe CollectionBase.List property, wecan specify the type of object that can be put into or pulled out of thecollection:
<Serializable()>_
Public ClassProductCollection
InheritsSystem.Collections.CollectionBase
'==============================================================
' Item Property - public - ReadOnly
'--------------------------------------------------------------
' Returns the Product with the specifiedIndex
'==============================================================
Default PublicReadOnly Property Item(ByVal Index As Int32) _
AsComponents.Product
Get
ReturnMe.List.Item(Index)
End Get
End Property
'==============================================================
' Add() Method
'--------------------------------------------------------------
' Adds the specified Product object to theCollectionBase.List
'==============================================================
Public SubAdd(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 andthe database. The data class uses SqlDataReaderto get the data. The data reader is passed into a CreateProduct helper method, where an instance of the Product class is created and returnedto either be returned to the caller or added to a ProductCollection object:
Dim _reader AsSqlDataReader = _cmd.ExecuteReader()
_reader.Read()
'Create the Productobject
_product =makeProduct(_reader)
'Close the SqlDataReaderwhen done
_reader.Close()
In the end, we have a Productobject or a ProductCollection objectpopulated with instances of Productobjects. Either of these objects can be returned from a Web service becausethey both implement the <Serializable>attribute (see the Service1.asmx class). Additionally, you can bind the ProductCollection object to any datacontrol (see the WebForm1.aspx Web form).
The files referenced in this article are available fordownload.
Doug Seven is a co-founder of http://www.dotnetjunkies, a content-basedonline training resource for .NET developers. He has been building applicationswith the .NET Framework since the summer of 2000 and has co-authored five booksrelated to the .NET Framework: ProgrammingData-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).