You want 3 different classes.
class User
class UserCollection
..
then, you want a third class.
class UserMaker (or soemthing like that , I call mine UserController , the
name doesn't matter that much)
User encapsulates the properties, methods and (sometimes) events of a User
UserCollection is a CollectionBase ( dot net 1.1) of User's. Look at
generics if you're in 2.0
UserMaker is a biz layer object.
It will contain a private method, which will take a IDataReader as the
argument, and return a UserCollection.
Here is the code where I create a PubsTitle (which is a title entry .. in
the pubs database)
PubsTitle equates to User in your scenario.
PubsTitleColllection equates to UserCollection in your scenario.
Fyi, Titles.Layouts.TitlesAllLayout.XXXXXXXXXX
are just constants referring to the ordinal position on the ResultSet.
// this is private, because you reuse this method ......
private BusinessObjectCollections.PubsTitleCollection
SerializeTitles(IDataReader dataReader)
{
BusinessObjects.PubsTitle item = new BusinessObjects.PubsTitle();
BusinessObjectCollections.PubsTitleCollection coll = new
BusinessObjectCollections.PubsTitleCollection();
try {
while (dataReader.Read()) {
if (!(dataReader.IsDBNull(Titles.Layouts.TitlesAllLayout.TITLEID))) {
item = new
BusinessObjects.PubsTitle(dataReader.GetString(Titles.Layouts.TitlesAllLayou
t.TITLEID), dataReader.GetString(Titles.Layouts.TitlesAllLayout.PUBID),
dataReader.GetString(Titles.Layouts.TitlesAllLayout.PUBNAME));
if (!(dataReader.IsDBNull(Titles.Layouts.TitlesAllLayout.TITLE))) {
item.Title =
dataReader.GetString(Titles.Layouts.TitlesAllLayout.TITLE);
}
coll.Add(item);
}
}
return coll;
} finally {
if (!((dataReader == null))) {
try {
dataReader.Close();
} catch {
}
}
}
}
By doing it this way, you can populate the IDataReader with
1 User
10 Users
100 Users
(aka, it doesn't matter, you'll always get a UserCollection back)
The biz layer will have multipe methods, here is a sample which gets a
single Title, based in the TitleID
public BusinessLogic.BusinessObjects.PubsTitle GetSingleTitle(string
titleId)
{
try {
Data.Titles.PubsTitlesData dataObject = new Data.Titles.PubsTitlesData();
BusinessLogic.BusinessObjects.PubsTitle returnValue=null;
returnValue =
this.SerializeTitles(dataObject.GetSingleTitleDataReader(titleId)[0];
//notice I use the indexer to return the first item of the collection, since
I expect 1 title
}
return returnValue;
} finally {
}
}
public BusinessLogic.BusinessObjects.PubsTitle GetTitlesAll()
{
try {
Data.Titles.PubsTitlesData dataObject = new Data.Titles.PubsTitlesData();
BusinessObjectCollections.PubsTitleCollection returnValue=null;
returnValue = this.SerializeTitles(dataObject.GetAllTitlesDataReader();
}
return returnValue;
} finally {
}
}
You can have several of these methods.
The object not defined is the
Data.Titles.PubsTitlesData
this is the datalayer object, which returns IDataReaders. (it also returns
DataSets, scalars, and voids, but those aren't germane to this discussion)
If you want a single User (or title from my example), then create an
IDataReader with 1 User in it.
If you want all Users, then create (in the datalayer) an IDataReader with
all Users in it.
If you want all Users with Lastname Of "Smith", then create an IDataReader
with Smith Users in it.
(you get the picture... you create an IDataReader for whatever need you
have)
..
The biz method will call the DataLayer, to get the appropriate IDataReader.
It will pass in into the generic "SerializeTitles" method, which will return
a Collection (UserCollection in your case) of User's.
see also
http://groups.google.com/group/micr...33/b6af85a2e6593dfa?lnk=st&q=IDataReader+idr&
rnum=2&hl=en#b6af85a2e6593dfa
By building a generic datalayer, you can switch databases without too much
heartache.... because any db can get a IDataReader created against it.
You biz layer uses IDataReaders, to loop over and create your business
objects and objectCollections.
This is a tried and true method for me. I can't imagine ever going back.
...........
Also that using method 2 will probably result in 2 SQL HITS.
For an example,
the first hit will get the "user ids" and return them to a
sqldatareader. The reader would loop through the user ids and intialize
each User class (SECOND hit) and add to the collection.