473,385 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Architecture with a DataReader

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?

--

Andrew Robinson
May 2 '06 #1
2 2091
Have you considerred looking at an O/R mapper like nhibernate, gentle.net,
or Wilson O/R that does this for you?

Cheers,

Greg
"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
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?

--

Andrew Robinson

May 3 '06 #2
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
May 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
14
by: Jacko | last post by:
Hi guys, Say I made a SELECT statement to my sql DB that would return 50 rows that I will use a sqldatareader to access. Instead of iterating through each and every row of the datareader, I'd...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
2
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also...
10
by: Julien | last post by:
Hi folks, I am facing a little problem with my website that gives me an headache : It can not have 2 users at the same time (quite annoying, as you see:)) I think I miss something in the ASP.NET...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
6
by: Kalim Julia | last post by:
Is it possible to open more than 1 datareader / dataadapter on one connection ? Is it possible to duplicate / clone connection ?
0
by: rsdev | last post by:
Hi, I am new to ASP.NET and I am trying to build a CMS application using theBeerHouse SK. I am adapting the Datareader to collect information from an SQL database and send it to an HtmlHeader. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.