473,657 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ADO.NET DataReaders and the Middle Tier

I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the
middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.
Nov 18 '05 #1
7 1780
you should not return datareaders, as this opens the dal to connection
leaks.

-- bruce (sqlwork.com)

"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ******@TK2MSFTN GP09.phx.gbl...
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the
middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.

Nov 18 '05 #2
My DAL returns a generic datareader. This way I can use Oracle or SQL Server
and still have just one DAL.

There are many overloaded methods (3 of 13 are shown below) that "forward"
the call to a method that has more paramters and fills them in with default
values (or config file values). Eventually you get to a method that actually
executes the command. (There are also many "helper" methods that are not
shown. Like PrepareCommand. )

Public Overloads Shared Function ExecuteReader(B yVal commandText As String)
As IDataReader
Return ExecuteReader(m ConnStr, CommandType.Tex t, commandText,
CType(Nothing, IDataParameter( )))
End Function

Public Overloads Shared Function ExecuteReader(B yVal spName As String, ByVal
ParamArray parameterValues () As Object) As IDataReader
Return ExecuteReader(m ConnStr, spName, CommandType.Sto redProcedure,
parameterValues )
End Function

Public Overloads Shared Function ExecuteReader(B yVal commandType As
CommandType, ByVal commandText As String) As IDataReader
Return ExecuteReader(m ConnStr, commandType, commandText,
CType(Nothing, IDataParameter( )))
End Function
=============== ============
All calls eventually end up here:

Private Overloads Shared Function ExecuteReader(B yVal connection As
IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
CommandType, ByVal commandText As String, ByVal commandParamete rs() As
IDataParameter, ByVal connectionOwner ship As ConnectionOwner ship) As
IDataReader
If (connection Is Nothing) Then Throw New
ArgumentNullExc eption("Missing connection")
Dim cmd As IDbCommand = CreateCommand()
Dim dr As IDataReader
Dim mustCloseConnec tion As Boolean = False

Try
PrepareCommand( cmd, connection, transaction, commandType,
commandText, commandParamete rs, mustCloseConnec tion)
If connectionOwner ship = connectionOwner ship.External Then
dr = cmd.ExecuteRead er()
Else
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
End If

'Detach the Parameters from the command object, so they can be used
again
Dim canClear As Boolean = True
Dim commandParamete r As IDataParameter
For Each commandParamete r In cmd.Parameters
If commandParamete r.Direction <> ParameterDirect ion.Input Then
canClear = False
End If
Next

If (canClear) Then cmd.Parameters. Clear()

Return dr
Catch
If (mustCloseConne ction) Then connection.Clos e()
Throw
End Try
End Function
--
Joe Fallon


"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the
middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.

Nov 18 '05 #3
Suggest you look at the latest version of Microsoft data access Application
Block (aka "SqlHelper" )
Even if you choose not to use this free code from MS,
it should provide numerous insights into how to structure your own
customized DAL
-Peter
"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the
middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.

Nov 18 '05 #4
Thanks for the great explanation and sample code.
What is the physical implementation of your DAL - is it on a separate
machine, or is it in a class in the same assembly as other application code?
Sorry if it should be obvious to me - but I've never done anything with
Remoting. If your DAL is not on a separate machine, how would the calling
code change? I plan to read the fine manual on Remoting next week - just
hoping for now to get the short version if you can provide that.

G

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
My DAL returns a generic datareader. This way I can use Oracle or SQL Server and still have just one DAL.

There are many overloaded methods (3 of 13 are shown below) that "forward"
the call to a method that has more paramters and fills them in with default values (or config file values). Eventually you get to a method that actually executes the command. (There are also many "helper" methods that are not
shown. Like PrepareCommand. )

Public Overloads Shared Function ExecuteReader(B yVal commandText As String) As IDataReader
Return ExecuteReader(m ConnStr, CommandType.Tex t, commandText,
CType(Nothing, IDataParameter( )))
End Function

Public Overloads Shared Function ExecuteReader(B yVal spName As String, ByVal ParamArray parameterValues () As Object) As IDataReader
Return ExecuteReader(m ConnStr, spName, CommandType.Sto redProcedure,
parameterValues )
End Function

Public Overloads Shared Function ExecuteReader(B yVal commandType As
CommandType, ByVal commandText As String) As IDataReader
Return ExecuteReader(m ConnStr, commandType, commandText,
CType(Nothing, IDataParameter( )))
End Function
=============== ============
All calls eventually end up here:

Private Overloads Shared Function ExecuteReader(B yVal connection As
IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
CommandType, ByVal commandText As String, ByVal commandParamete rs() As
IDataParameter, ByVal connectionOwner ship As ConnectionOwner ship) As
IDataReader
If (connection Is Nothing) Then Throw New
ArgumentNullExc eption("Missing connection")
Dim cmd As IDbCommand = CreateCommand()
Dim dr As IDataReader
Dim mustCloseConnec tion As Boolean = False

Try
PrepareCommand( cmd, connection, transaction, commandType,
commandText, commandParamete rs, mustCloseConnec tion)
If connectionOwner ship = connectionOwner ship.External Then
dr = cmd.ExecuteRead er()
Else
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
End If

'Detach the Parameters from the command object, so they can be used again
Dim canClear As Boolean = True
Dim commandParamete r As IDataParameter
For Each commandParamete r In cmd.Parameters
If commandParamete r.Direction <> ParameterDirect ion.Input Then
canClear = False
End If
Next

If (canClear) Then cmd.Parameters. Clear()

Return dr
Catch
If (mustCloseConne ction) Then connection.Clos e()
Throw
End Try
End Function
--
Joe Fallon


"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.


Nov 18 '05 #5
From an architectural perspective, I'm with Bruce on this one.
You may end up killing scaleabilty of your solution if you pass DataReaders
to your client.
General advice is to avoid 1 database connection per client if at all
possible.

Below are a couple of good MSDN articles covering best practices.

Designing Data Tier Components and Passing Data Through Tiers:
http://msdn.microsoft.com/library/de...tml/BOAGag.asp

..NET Data Access Architecture Guide:
http://msdn.microsoft.com/library/de.../html/daag.asp
"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from the
middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.

Nov 18 '05 #6
The DAL is a separate class library: DAL.dll
The application makes reference to it.

The DAL.dll *always* resides on the machine that has access to the database.
So *it* is never involved in remoting.

Business objects can be remoted to the machine where the DAL lives.
(Or if they are the same machine then no remoting is required.)
The BOs use calls to the DAL rather than writing blocks of ADO.Net code
inside of them.

Hope that helps.
--
Joe Fallon

"Guadala Harry" <gh****@aol.com > wrote in message
news:O8******** ********@tk2msf tngp13.phx.gbl. ..
Thanks for the great explanation and sample code.
What is the physical implementation of your DAL - is it on a separate
machine, or is it in a class in the same assembly as other application code? Sorry if it should be obvious to me - but I've never done anything with
Remoting. If your DAL is not on a separate machine, how would the calling
code change? I plan to read the fine manual on Remoting next week - just
hoping for now to get the short version if you can provide that.

G

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
My DAL returns a generic datareader. This way I can use Oracle or SQL

Server
and still have just one DAL.

There are many overloaded methods (3 of 13 are shown below) that "forward"
the call to a method that has more paramters and fills them in with

default
values (or config file values). Eventually you get to a method that

actually
executes the command. (There are also many "helper" methods that are not
shown. Like PrepareCommand. )

Public Overloads Shared Function ExecuteReader(B yVal commandText As

String)
As IDataReader
Return ExecuteReader(m ConnStr, CommandType.Tex t, commandText,
CType(Nothing, IDataParameter( )))
End Function

Public Overloads Shared Function ExecuteReader(B yVal spName As String,

ByVal
ParamArray parameterValues () As Object) As IDataReader
Return ExecuteReader(m ConnStr, spName, CommandType.Sto redProcedure, parameterValues )
End Function

Public Overloads Shared Function ExecuteReader(B yVal commandType As
CommandType, ByVal commandText As String) As IDataReader
Return ExecuteReader(m ConnStr, commandType, commandText,
CType(Nothing, IDataParameter( )))
End Function
=============== ============
All calls eventually end up here:

Private Overloads Shared Function ExecuteReader(B yVal connection As
IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
CommandType, ByVal commandText As String, ByVal commandParamete rs() As
IDataParameter, ByVal connectionOwner ship As ConnectionOwner ship) As
IDataReader
If (connection Is Nothing) Then Throw New
ArgumentNullExc eption("Missing connection")
Dim cmd As IDbCommand = CreateCommand()
Dim dr As IDataReader
Dim mustCloseConnec tion As Boolean = False

Try
PrepareCommand( cmd, connection, transaction, commandType,
commandText, commandParamete rs, mustCloseConnec tion)
If connectionOwner ship = connectionOwner ship.External Then
dr = cmd.ExecuteRead er()
Else
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
End If

'Detach the Parameters from the command object, so they can be

used
again
Dim canClear As Boolean = True
Dim commandParamete r As IDataParameter
For Each commandParamete r In cmd.Parameters
If commandParamete r.Direction <> ParameterDirect ion.Input Then
canClear = False
End If
Next

If (canClear) Then cmd.Parameters. Clear()

Return dr
Catch
If (mustCloseConne ction) Then connection.Clos e()
Throw
End Try
End Function
--
Joe Fallon


"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm trying to design all of my data access logic into one centralized
assembly. I'm wondering how to implement DataReaders.
There's plenty of documentation on passing DataSets to the client from

the middle tier... but what about DataReaders? Do I have to bypass the
centralized data access assembly when I want to use DataReaders?

Thanks.



Nov 18 '05 #7
Yes - that helps.
You've saved me a bunch of time.
Thanks.

G

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:uH******** *******@TK2MSFT NGP09.phx.gbl.. .
The DAL is a separate class library: DAL.dll
The application makes reference to it.

The DAL.dll *always* resides on the machine that has access to the database. So *it* is never involved in remoting.

Business objects can be remoted to the machine where the DAL lives.
(Or if they are the same machine then no remoting is required.)
The BOs use calls to the DAL rather than writing blocks of ADO.Net code
inside of them.

Hope that helps.
--
Joe Fallon

"Guadala Harry" <gh****@aol.com > wrote in message
news:O8******** ********@tk2msf tngp13.phx.gbl. ..
Thanks for the great explanation and sample code.
What is the physical implementation of your DAL - is it on a separate
machine, or is it in a class in the same assembly as other application

code?
Sorry if it should be obvious to me - but I've never done anything with
Remoting. If your DAL is not on a separate machine, how would the calling
code change? I plan to read the fine manual on Remoting next week - just
hoping for now to get the short version if you can provide that.

G

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
My DAL returns a generic datareader. This way I can use Oracle or SQL

Server
and still have just one DAL.

There are many overloaded methods (3 of 13 are shown below) that "forward" the call to a method that has more paramters and fills them in with

default
values (or config file values). Eventually you get to a method that

actually
executes the command. (There are also many "helper" methods that are not shown. Like PrepareCommand. )

Public Overloads Shared Function ExecuteReader(B yVal commandText As

String)
As IDataReader
Return ExecuteReader(m ConnStr, CommandType.Tex t, commandText,
CType(Nothing, IDataParameter( )))
End Function

Public Overloads Shared Function ExecuteReader(B yVal spName As String,

ByVal
ParamArray parameterValues () As Object) As IDataReader
Return ExecuteReader(m ConnStr, spName, CommandType.Sto redProcedure, parameterValues )
End Function

Public Overloads Shared Function ExecuteReader(B yVal commandType As
CommandType, ByVal commandText As String) As IDataReader
Return ExecuteReader(m ConnStr, commandType, commandText,
CType(Nothing, IDataParameter( )))
End Function
=============== ============
All calls eventually end up here:

Private Overloads Shared Function ExecuteReader(B yVal connection As
IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As CommandType, ByVal commandText As String, ByVal commandParamete rs() As
IDataParameter, ByVal connectionOwner ship As ConnectionOwner ship) As
IDataReader
If (connection Is Nothing) Then Throw New
ArgumentNullExc eption("Missing connection")
Dim cmd As IDbCommand = CreateCommand()
Dim dr As IDataReader
Dim mustCloseConnec tion As Boolean = False

Try
PrepareCommand( cmd, connection, transaction, commandType,
commandText, commandParamete rs, mustCloseConnec tion)
If connectionOwner ship = connectionOwner ship.External Then
dr = cmd.ExecuteRead er()
Else
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
End If

'Detach the Parameters from the command object, so they can be

used
again
Dim canClear As Boolean = True
Dim commandParamete r As IDataParameter
For Each commandParamete r In cmd.Parameters
If commandParamete r.Direction <> ParameterDirect ion.Input Then canClear = False
End If
Next

If (canClear) Then cmd.Parameters. Clear()

Return dr
Catch
If (mustCloseConne ction) Then connection.Clos e()
Throw
End Try
End Function
--
Joe Fallon


"Guadala Harry" <gh****@aol.com > wrote in message
news:e6******** ********@TK2MSF TNGP09.phx.gbl. ..
> I'm trying to design all of my data access logic into one centralized > assembly. I'm wondering how to implement DataReaders.
> There's plenty of documentation on passing DataSets to the client

from the
> middle tier... but what about DataReaders? Do I have to bypass the
> centralized data access assembly when I want to use DataReaders?
>
> Thanks.
>
>



Nov 18 '05 #8

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

Similar topics

10
2913
by: Kumar Saurabh | last post by:
Hi Devs, I was thinking of implementing a middle tire (most probably in XML) for a PHP MySQL web app but couldn't make a head way. can any one suggest how can i proceed with this. Thanks in advance.
62
4093
by: SAN3141 | last post by:
There doesn't seem to be consensus about when to put code in the database or in the middle tier. There was a long discussion about this in an Oracle newsgroup (message ID: ULcQb.466$KU5.37@nwrddc02.gnilink.net). Elsewhere there's been discussion about Microsoft SQL Server 2005 adding the CLR to support stored procedures in languages such as C#. A scan of the Web and discussion forums finds differing opinions about this.
2
2046
by: billym | last post by:
Does anyone know if C# (or .NET applications in general) can be load balanced on the middle tier simply by deploying them to multiple servers in a clustered environtment? If so, would this be something supported by the Framework, MSCS, or a third party solution such as Local Director? Also, if it can be done are their coding caveats that must be adhered to?
0
1346
by: JasonP | last post by:
Hi, I am trying to decide the best middle tier method for the system I am currently building. There are no issues of supporting previous software versions, etc as this is a completely new company. The company in question currently has 10 client pcs (which is likely to grow rapidly), each of which will need to get some data from a database, display it on screen for editing and of course write it back. The data has to be distributed in...
5
1174
by: Ryan Ternier | last post by:
I know how this should be done in regards to nTier, but it seems a bit inneficient, and was wondering if there's a solution that I havn't thought of yet. (I'm switching this loop to For Each Row as DataRow in.... to kill the one int that's not needed.) For intCount1 = 0 To objData.DataSet.Tables("tblSecondaryNumbers").Rows.Count - 1 rowTemp = objData.DataSet.Tables("tblSecondaryNumbers").Rows(intCount1)
6
2247
by: Ian Williamson | last post by:
Greetings, My company has an ASP.NET based enterprise product that is undergoing some changes and I need some community input to help solve a problem. In the current implementation, any given installation of the product supports only one database at a time. The server/db information is stored in the registry on the computer hosting the com+ based middle tier. We are now moving to a solution which supports multiple databases. That...
1
1238
by: dgk | last post by:
How can I create a middle tier object using the standard databinding stuff? All the examples that I see in the books using the new 2005 controls just shows how to build stuff in two tiers. It writes all the update logic, which is great, but it goes straight from the grid to the database. Do I try to inherit from a DataAdapter and put my own code in there?
3
1709
by: Jules | last post by:
Sorry for being contentious title, but I am confused and concerned over the level of MS infrastructure support to the business logic layer. MS have great support to Web services, for EAI and B2B Integration, ASP for Web pages. We also have SQL Server, ADO, data sets etc for great Data services layer. Unfortunalely most of the Architectural (Patterns, papers, Samples etc) emphasis seem to be biased torwards web based applications and...
3
4333
by: SevDer | last post by:
Hello All, For my ASP.NET application, I am trying to have my middle tier (currently logical layer) dll's to be loaded dynamically. The aim is not to kill the sessions when I need to update those DLLs. (I know that, I can stop app recycle when bin, web.config or folder changes) Ideally, I would like load balance my middle tier dll's from a load balanced machines. Now, I am trying to findout which way is the best way to go.
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.