473,404 Members | 2,137 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,404 software developers and data experts.

OOP advice needed

I've got an ASP.NET web site that primarily uses the SqlDataSource for data
access tasks, and I am in the process of migrating it to use Business
Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my first
bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called FillDataRecord,
and a basic method that calls a proc to get the top 15 articles:

public static ArticleList GetList()
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListForFrontPage");
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

Now I need to return a list of articles by CategoryID:

public static ArticleList GetCategoryList(int categoryid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByCategory");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", categoryid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

And again by ArticleTypeID:

public static ArticleList GetArticleTypeList(int articletypeid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByArticleType");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", articletypeid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

The three methods are almost identical, and seem a good candidate for some
consolidation of code. However, I don't have a clue how I should go about
it. Any suggestions?

Thanks

Mike
Jun 26 '07 #1
13 1441
Ysgrifennodd Mike:
I've got an ASP.NET web site that primarily uses the SqlDataSource for data
access tasks, and I am in the process of migrating it to use Business
Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my first
bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called FillDataRecord,
and a basic method that calls a proc to get the top 15 articles:

Is there any reason why you can't use DataSets (or Typed DataSets, which
are even better IMHO)? It's what they're for.

Re-usability and all that ;)
Peter
Jun 26 '07 #2
really isnt an OOP answer, but its not really appropriate here :)

Try writing a function that with this signature

private static ArticleList GetArticleList(string storedProcedureName,
List<SqlParameterparameters)...

then wrap this function with each of the ones you have above.


Jun 26 '07 #3

"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:7_*********************@pipex.net...
Ysgrifennodd Mike:
>I've got an ASP.NET web site that primarily uses the SqlDataSource for
data access tasks, and I am in the process of migrating it to use
Business Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my
first bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called
FillDataRecord, and a basic method that calls a proc to get the top 15
articles:


Is there any reason why you can't use DataSets (or Typed DataSets, which
are even better IMHO)? It's what they're for.

Re-usability and all that ;)
None at all, except that I haven't looked at that yet (Typed Datasets). I'm
trying to get my head around this approach first.

Mike
Jun 26 '07 #4
Mike <bl***@blank.blankwrote:

<snip>
The three methods are almost identical, and seem a good candidate for some
consolidation of code. However, I don't have a clue how I should go about
it. Any suggestions?
The only bit which changes is the SqlCommand, so create a method which
accepts a SqlCommand and does the rest.

(I don't think the code you've actually posted would compile, and the
code doesn't appear to associate the SqlCommand with the connection,
but in general you don't need to call Close on things if you've already
got them in a using statement.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 26 '07 #5
Ysgrifennodd Mike:
>
None at all, except that I haven't looked at that yet (Typed Datasets). I'm
trying to get my head around this approach first.

Mike

Hi Mike,

Been there. Done that. We originally went for returning simpler data
structures because we thought they'd be easier. We were wrong. It's a
couple of years ago, now, but I do recall that we put a stop to it after
our first project and settled for DataSets, where it's all done for you.

The only exception to this was a .NET 1.1 application that our managers
in their wisdom decided we didn't have time to do and outsourced to a
major contractor/consultancy. The code is the worst you've ever seen in
your life. It contains, for example, code to convert a DataRow in a
DataSet to an Array of Object (Ahem! It already *is* an array of
object!) We'd still be laughing if we didn't have to maintain it.

We now use Typed DataSets. If you want to know about Typed DataSets,
take a look at:

http://www.peredur.uklinux.net/TypedDataSets.pdf

(That version may be a bit out-of-date by now. If it is, I'll try to
remember to update it. But it should give you a start.)

HTH
Peter
Jun 26 '07 #6

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Mike <bl***@blank.blankwrote:

<snip>
>The three methods are almost identical, and seem a good candidate for
some
consolidation of code. However, I don't have a clue how I should go
about
it. Any suggestions?

The only bit which changes is the SqlCommand, so create a method which
accepts a SqlCommand and does the rest.

(I don't think the code you've actually posted would compile, and the
code doesn't appear to associate the SqlCommand with the connection,
but in general you don't need to call Close on things if you've already
got them in a using statement.)
It compiles ok, but you are right. It is incomplete and wouldn't run as-is
if I tried it.

Thanks for the suggestion and the tip about Close in a using statement.

Mike
Jun 26 '07 #7
Mike <bl***@blank.blankwrote:
(I don't think the code you've actually posted would compile, and the
code doesn't appear to associate the SqlCommand with the connection,
but in general you don't need to call Close on things if you've already
got them in a using statement.)

It compiles ok, but you are right. It is incomplete and wouldn't run as-is
if I tried it.
The code as you posted it would *not* compile - you're using both conn
and sdr outside the scope they're declared in. You should be getting
these errors (which I do when compiling a test program based on what
you posted):

Test.cs(23,13): error CS0103: The name 'sdr' does not exist in the
current context
Test.cs(25,9): error CS0103: The name 'conn' does not exist in the
current context

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 26 '07 #8
PS

"Mike" <bl***@blank.blankwrote in message
news:el**************@TK2MSFTNGP06.phx.gbl...
>
"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:7_*********************@pipex.net...
>Ysgrifennodd Mike:
>>I've got an ASP.NET web site that primarily uses the SqlDataSource for
data access tasks, and I am in the process of migrating it to use
Business Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my
first bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called
FillDataRecord, and a basic method that calls a proc to get the top 15
articles:


Is there any reason why you can't use DataSets (or Typed DataSets, which
are even better IMHO)? It's what they're for.

Re-usability and all that ;)

None at all, except that I haven't looked at that yet (Typed Datasets).
I'm trying to get my head around this approach first.
Datasets and business objects are different approachs to modelling your
application. If you are using a domain model approach then use business
objects. If you are using a more table oriented approach typically found in
CRUD type applications then use datasets.

PS
Mike

Jun 27 '07 #9
see
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry
"Mike" <bl***@blank.blankwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
I've got an ASP.NET web site that primarily uses the SqlDataSource for
data access tasks, and I am in the process of migrating it to use Business
Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my first
bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called
FillDataRecord, and a basic method that calls a proc to get the top 15
articles:

public static ArticleList GetList()
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListForFrontPage");
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

Now I need to return a list of articles by CategoryID:

public static ArticleList GetCategoryList(int categoryid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByCategory");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", categoryid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

And again by ArticleTypeID:

public static ArticleList GetArticleTypeList(int articletypeid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByArticleType");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", articletypeid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}

The three methods are almost identical, and seem a good candidate for some
consolidation of code. However, I don't have a clue how I should go about
it. Any suggestions?

Thanks

Mike

Jun 27 '07 #10

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Mike <bl***@blank.blankwrote:
(I don't think the code you've actually posted would compile, and the
code doesn't appear to associate the SqlCommand with the connection,
but in general you don't need to call Close on things if you've already
got them in a using statement.)

It compiles ok, but you are right. It is incomplete and wouldn't run
as-is
if I tried it.

The code as you posted it would *not* compile -
Absolutely right, John

It compiled ok because I *had* made those changes - after copying and
pasting into the post, but before sending the post. I forgot to update the
code in the post before clicking send....
Jun 27 '07 #11

"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:Q5******************************@pipex.net...
Ysgrifennodd Mike:
>>
None at all, except that I haven't looked at that yet (Typed Datasets).
I'm trying to get my head around this approach first.

Mike

Hi Mike,

Been there. Done that. We originally went for returning simpler data
structures because we thought they'd be easier. We were wrong. It's a
couple of years ago, now, but I do recall that we put a stop to it after
our first project and settled for DataSets, where it's all done for you.

The only exception to this was a .NET 1.1 application that our managers in
their wisdom decided we didn't have time to do and outsourced to a major
contractor/consultancy. The code is the worst you've ever seen in your
life. It contains, for example, code to convert a DataRow in a DataSet to
an Array of Object (Ahem! It already *is* an array of object!) We'd
still be laughing if we didn't have to maintain it.

We now use Typed DataSets. If you want to know about Typed DataSets, take
a look at:

http://www.peredur.uklinux.net/TypedDataSets.pdf

(That version may be a bit out-of-date by now. If it is, I'll try to
remember to update it. But it should give you a start.)

HTH
I will be trying the typed dataset approach next. I'm trying a number of
approaches so I can familiarise myself with what is involved in each one.
I come from a classic ASP background, so the ADO.NET code in the code-behind
approach is one I'm very comfortable with, and I don't have to worry about
others being able to maintain what I write (bit like your contactors LOL).
SqlDataSources seem brilliant to me for getting a simple app up and running
double quick, but sometimes still need ADO.NET code added, and I find the
mixture of ADO.NET and SqlDataSources "messy".

At the moment, I confess that I'm struggling to see the advantages of using
Business Objects...

Thanks

Mike
Jun 27 '07 #12
"Mike" <bl***@blank.blankwrote in message
news:et**************@TK2MSFTNGP02.phx.gbl...
>
"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:Q5******************************@pipex.net...
>Ysgrifennodd Mike:
>>>
None at all, except that I haven't looked at that yet (Typed Datasets).
I'm trying to get my head around this approach first.

Mike

Hi Mike,

Been there. Done that. We originally went for returning simpler data
structures because we thought they'd be easier. We were wrong. It's a
couple of years ago, now, but I do recall that we put a stop to it after
our first project and settled for DataSets, where it's all done for you.

The only exception to this was a .NET 1.1 application that our managers
in their wisdom decided we didn't have time to do and outsourced to a
major contractor/consultancy. The code is the worst you've ever seen in
your life. It contains, for example, code to convert a DataRow in a
DataSet to an Array of Object (Ahem! It already *is* an array of
object!) We'd still be laughing if we didn't have to maintain it.

We now use Typed DataSets. If you want to know about Typed DataSets,
take a look at:

http://www.peredur.uklinux.net/TypedDataSets.pdf

(That version may be a bit out-of-date by now. If it is, I'll try to
remember to update it. But it should give you a start.)

HTH

I will be trying the typed dataset approach next. I'm trying a number of
approaches so I can familiarise myself with what is involved in each one.
I come from a classic ASP background, so the ADO.NET code in the
code-behind approach is one I'm very comfortable with, and I don't have to
worry about others being able to maintain what I write (bit like your
contactors LOL). SqlDataSources seem brilliant to me for getting a simple
app up and running double quick, but sometimes still need ADO.NET code
added, and I find the mixture of ADO.NET and SqlDataSources "messy".

At the moment, I confess that I'm struggling to see the advantages of
using Business Objects...
I think the biggest advantage of Business Objects is a place to add
additional logic beyond what's in the db. Functionality that "belongs" with
the object can be included with the object. I find it keeps the code cleaner
and easier to understand for future revisions.
Jun 27 '07 #13

"PS" <ec***********@hotmail.comwrote in message
news:uq****************@TK2MSFTNGP04.phx.gbl...
>
"Mike" <bl***@blank.blankwrote in message
news:el**************@TK2MSFTNGP06.phx.gbl...
>>
"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:7_*********************@pipex.net...
>>Ysgrifennodd Mike:
I've got an ASP.NET web site that primarily uses the SqlDataSource for
data access tasks, and I am in the process of migrating it to use
Business Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my
first bash at OOP and I've hit a stumbling block.

I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called
FillDataRecord, and a basic method that calls a proc to get the top 15
articles:

Is there any reason why you can't use DataSets (or Typed DataSets, which
are even better IMHO)? It's what they're for.

Re-usability and all that ;)

None at all, except that I haven't looked at that yet (Typed Datasets).
I'm trying to get my head around this approach first.

Datasets and business objects are different approachs to modelling your
application. If you are using a domain model approach then use business
objects. If you are using a more table oriented approach typically found
in CRUD type applications then use datasets.
Your reply got me googling. Now I'm beginning to see the theory behind the
different approaches. Fascinating. Thank you very much for your
contribution.

Mike
Jun 28 '07 #14

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

Similar topics

75
by: Howard Nease | last post by:
Hello, everyone. I would appreciate any advice that someone could give me on my future career path. Here is my situation: I am a bright Junior in a very well-respected private high school, taking...
11
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless...
47
by: ship | last post by:
Hi We need some advice: We are thinking of upgrading our Access database from Access 2000 to Access 2004. How stable is MS Office 2003? (particularly Access 2003). We are just a small...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
by: J Rieggle | last post by:
Hi there, I am stuck on a problem that relates to eCommerce sites, but isnt ASP.NET specific (sorry). The ecommerce site is working in the UK, and products will be sold in pounds stirling. ...
8
by: george.leithead | last post by:
Hi all, I'm looking for some advice on how best to achitect the following requirement. I'm basically writing a Fantasy Football (FF) Web site, and would like to have it fully OO and have it...
28
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to...
1
by: iKiLL | last post by:
Hi All, I am new to .Net and Compact Framework. So I have a really basic question for you all. I am building an application in C# for Windows Mobile 5.0 in VS2005 CF2.0
7
by: SM | last post by:
Hello, I have a index.php template (2 columns). The right columns contains a bunch of links (interviews, poems, etc...) The left columns contains the actual article. So if I click on a link on...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.