A
Andrew Robinson
I am working on a data access layer using a pattern that I see more and
more. Define a class that is a data container that is then added to a
generic List<>. I am then using this List and support classes in conjunction
with an objectDataSource control.
public class Item
{
public Item() {
}
private int itemID;
public int ItemID {
get { return itemID; }
set { itemID = value; }
}
private string code;
public string Code {
get { return code; }
set { code = value; }
}
...
the issue that I am struggling with is that I added another column to my
data table and corresponding property to my Item class and am using 5 or 6
different data readers to load up my List depending on sort order, selection
conditions etc. I forgot to add the logic for one of the corresponding
properties to one of my readers and wound up chasing the resulting null
field / bug for 1/2 a day. I see two solutions:
1. create a class or method within my DAL that is passed an open dataReader
and that returns a List<>. This would have the advantage of requiring only
one place to change my logic but I know that passing open dataReaders is a
bad practice.
2. create a constructor for my Item class that includes all of the possible
field initilizers. If I add a field and property to the class, any
dataReader logic that doesn't include the additional parameter will fail. I
like this, but my Item class might have 10 or more properties that need to
be set and the code for this when newing up the object just looks plain
ugly. I hate using more than 5 or 6 parameters on a method.
suggestions?
more. Define a class that is a data container that is then added to a
generic List<>. I am then using this List and support classes in conjunction
with an objectDataSource control.
public class Item
{
public Item() {
}
private int itemID;
public int ItemID {
get { return itemID; }
set { itemID = value; }
}
private string code;
public string Code {
get { return code; }
set { code = value; }
}
...
the issue that I am struggling with is that I added another column to my
data table and corresponding property to my Item class and am using 5 or 6
different data readers to load up my List depending on sort order, selection
conditions etc. I forgot to add the logic for one of the corresponding
properties to one of my readers and wound up chasing the resulting null
field / bug for 1/2 a day. I see two solutions:
1. create a class or method within my DAL that is passed an open dataReader
and that returns a List<>. This would have the advantage of requiring only
one place to change my logic but I know that passing open dataReaders is a
bad practice.
2. create a constructor for my Item class that includes all of the possible
field initilizers. If I add a field and property to the class, any
dataReader logic that doesn't include the additional parameter will fail. I
like this, but my Item class might have 10 or more properties that need to
be set and the code for this when newing up the object just looks plain
ugly. I hate using more than 5 or 6 parameters on a method.
suggestions?