Architecture with a DataReader

  • Thread starter Andrew Robinson
  • Start date
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?
 
G

Greg Young

Have you considerred looking at an O/R mapper like nhibernate, gentle.net,
or Wilson O/R that does this for you?

Cheers,

Greg
 
R

Russell Mangel

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ExampleDataReader
{
class Program
{
static void Main(string[] args)
{
// Example1
Collection<Item> collectionItems = GetCollectionItems();
// Loop through Collection Items
foreach (Item item in collectionItems)
{
Console.WriteLine(item.ItemID + " " + item.Code);
}
Console.WriteLine(Environment.NewLine);

// Example2
List<Item> listItems = GetListItems();
// Loop through List Items
foreach (Item item in listItems)
{
Console.WriteLine(item.ItemID + " " + item.Code);
}
Console.WriteLine(Environment.NewLine);

// Example3
BindingList<Item> bindingListItems = GetBindingListItems();
// Loop through BindingList Items
foreach (Item item in bindingListItems)
{
Console.WriteLine(item.ItemID + " " + item.Code);
}
Console.WriteLine(Environment.NewLine);

// The for loop in each of the examples could instead
// be a loop using SqlDataReader which receives
// its data from a database.

// The important thing to understand is that there
// Are several collections, and you should use
// only the collection that suits your needs.
// Meaning, if you don't need to sort, or don't
// need to bind to a datagrid use. Collection<T>.

// From your original message you apparently
// need help with passing data from the methods.
// Each of these methods just returns the list
// of items that you need.

// If you had a datagridview control on a form.
// Try this:
// dataGridView1.DataSource = GetBindingListItems();
}

public static Collection<Item> GetCollectionItems()
{
// This demostrates the Generics Collection
// Use it when you just need a collection with no fancy stuff.
Collection<Item> collectionItems = new Collection<Item>();
Item item = null;

for (int i = 0; i < 10; i++)
{
item = new Item();
item.Code = Guid.NewGuid().ToString();
item.ItemID = i;

collectionItems.Add(item);
}

return collectionItems;
}
public static List<Item> GetListItems()
{
// This demostrates the Generics List Collection
// Use it when you just need to do more with the returned List of items.
List<Item> listItems = new List<Item>();
Item item = null;

for (int i = 0; i < 10; i++)
{
item = new Item();
item.Code = Guid.NewGuid().ToString();
item.ItemID = i;

listItems.Add(item);
}

return listItems;
}
public static BindingList<Item> GetBindingListItems()
{
// This demostrates the Generics BindingList Collection
// Use it when you need to do everything List can do,
// and when you want to bind the items to a dataGridView Control.

// If you are using SqlDataReader, replace the for loop below
// with your SqlDataReader loop. Then connect the dataGridView
// Datasource to it.

BindingList<Item> bindingListItems = new BindingList<Item>();
Item item = null;

for (int i = 0; i < 10; i++)
{
item = new Item();
item.Code = Guid.NewGuid().ToString();
item.ItemID = i;

bindingListItems.Add(item);
}

return bindingListItems;
}
}
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;
}
}
}
}
// End of Source Code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top