Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ADO.NET DataReaders and the Middle Tier

Reply
Thread Tools

ADO.NET DataReaders and the Middle Tier

 
 
Guadala Harry
Guest
Posts: n/a
 
      02-21-2004
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.


 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      02-21-2004
you should not return datareaders, as this opens the dal to connection
leaks.

-- bruce (sqlwork.com)

"Guadala Harry" <> wrote in message
news:e6Oe0OB#...
> 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.
>
>



 
Reply With Quote
 
 
 
 
Joe Fallon
Guest
Posts: n/a
 
      02-21-2004
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(ByVal commandText As String)
As IDataReader
Return ExecuteReader(mConnStr, CommandType.Text, commandText,
CType(Nothing, IDataParameter()))
End Function

Public Overloads Shared Function ExecuteReader(ByVal spName As String, ByVal
ParamArray parameterValues() As Object) As IDataReader
Return ExecuteReader(mConnStr, spName, CommandType.StoredProcedure,
parameterValues)
End Function

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

Private Overloads Shared Function ExecuteReader(ByVal connection As
IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
CommandType, ByVal commandText As String, ByVal commandParameters() As
IDataParameter, ByVal connectionOwnership As ConnectionOwnership) As
IDataReader
If (connection Is Nothing) Then Throw New
ArgumentNullException("Missing connection")
Dim cmd As IDbCommand = CreateCommand()
Dim dr As IDataReader
Dim mustCloseConnection As Boolean = False

Try
PrepareCommand(cmd, connection, transaction, commandType,
commandText, commandParameters, mustCloseConnection)
If connectionOwnership = connectionOwnership.External Then
dr = cmd.ExecuteReader()
Else
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
End If

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

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

Return dr
Catch
If (mustCloseConnection) Then connection.Close()
Throw
End Try
End Function
--
Joe Fallon




"Guadala Harry" <> wrote in message
news:e6Oe0OB%...
> 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.
>
>



 
Reply With Quote
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      02-21-2004
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" <> wrote in message
news:e6Oe0OB%...
> 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.
>
>



 
Reply With Quote
 
Guadala Harry
Guest
Posts: n/a
 
      02-21-2004
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" <> wrote in message
news:%23bcsaRC%...
> 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(ByVal commandText As

String)
> As IDataReader
> Return ExecuteReader(mConnStr, CommandType.Text, commandText,
> CType(Nothing, IDataParameter()))
> End Function
>
> Public Overloads Shared Function ExecuteReader(ByVal spName As String,

ByVal
> ParamArray parameterValues() As Object) As IDataReader
> Return ExecuteReader(mConnStr, spName, CommandType.StoredProcedure,
> parameterValues)
> End Function
>
> Public Overloads Shared Function ExecuteReader(ByVal commandType As
> CommandType, ByVal commandText As String) As IDataReader
> Return ExecuteReader(mConnStr, commandType, commandText,
> CType(Nothing, IDataParameter()))
> End Function
> ===========================
> All calls eventually end up here:
>
> Private Overloads Shared Function ExecuteReader(ByVal connection As
> IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
> CommandType, ByVal commandText As String, ByVal commandParameters() As
> IDataParameter, ByVal connectionOwnership As ConnectionOwnership) As
> IDataReader
> If (connection Is Nothing) Then Throw New
> ArgumentNullException("Missing connection")
> Dim cmd As IDbCommand = CreateCommand()
> Dim dr As IDataReader
> Dim mustCloseConnection As Boolean = False
>
> Try
> PrepareCommand(cmd, connection, transaction, commandType,
> commandText, commandParameters, mustCloseConnection)
> If connectionOwnership = connectionOwnership.External Then
> dr = cmd.ExecuteReader()
> Else
> dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
> End If
>
> 'Detach the Parameters from the command object, so they can be

used
> again
> Dim canClear As Boolean = True
> Dim commandParameter As IDataParameter
> For Each commandParameter In cmd.Parameters
> If commandParameter.Direction <> ParameterDirection.Input Then
> canClear = False
> End If
> Next
>
> If (canClear) Then cmd.Parameters.Clear()
>
> Return dr
> Catch
> If (mustCloseConnection) Then connection.Close()
> Throw
> End Try
> End Function
> --
> Joe Fallon
>
>
>
>
> "Guadala Harry" <> wrote in message
> news:e6Oe0OB%...
> > 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.
> >
> >

>
>



 
Reply With Quote
 
richlm
Guest
Posts: n/a
 
      02-21-2004
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" <> wrote in message
news:e6Oe0OB%...
> 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.
>
>



 
Reply With Quote
 
Joe Fallon
Guest
Posts: n/a
 
      02-21-2004
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" <> wrote in message
news:O8$eq1E%...
> 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" <> wrote in message
> news:%23bcsaRC%...
> > 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(ByVal commandText As

> String)
> > As IDataReader
> > Return ExecuteReader(mConnStr, CommandType.Text, commandText,
> > CType(Nothing, IDataParameter()))
> > End Function
> >
> > Public Overloads Shared Function ExecuteReader(ByVal spName As String,

> ByVal
> > ParamArray parameterValues() As Object) As IDataReader
> > Return ExecuteReader(mConnStr, spName,

CommandType.StoredProcedure,
> > parameterValues)
> > End Function
> >
> > Public Overloads Shared Function ExecuteReader(ByVal commandType As
> > CommandType, ByVal commandText As String) As IDataReader
> > Return ExecuteReader(mConnStr, commandType, commandText,
> > CType(Nothing, IDataParameter()))
> > End Function
> > ===========================
> > All calls eventually end up here:
> >
> > Private Overloads Shared Function ExecuteReader(ByVal connection As
> > IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType As
> > CommandType, ByVal commandText As String, ByVal commandParameters() As
> > IDataParameter, ByVal connectionOwnership As ConnectionOwnership) As
> > IDataReader
> > If (connection Is Nothing) Then Throw New
> > ArgumentNullException("Missing connection")
> > Dim cmd As IDbCommand = CreateCommand()
> > Dim dr As IDataReader
> > Dim mustCloseConnection As Boolean = False
> >
> > Try
> > PrepareCommand(cmd, connection, transaction, commandType,
> > commandText, commandParameters, mustCloseConnection)
> > If connectionOwnership = connectionOwnership.External Then
> > dr = cmd.ExecuteReader()
> > Else
> > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
> > End If
> >
> > 'Detach the Parameters from the command object, so they can be

> used
> > again
> > Dim canClear As Boolean = True
> > Dim commandParameter As IDataParameter
> > For Each commandParameter In cmd.Parameters
> > If commandParameter.Direction <> ParameterDirection.Input Then
> > canClear = False
> > End If
> > Next
> >
> > If (canClear) Then cmd.Parameters.Clear()
> >
> > Return dr
> > Catch
> > If (mustCloseConnection) Then connection.Close()
> > Throw
> > End Try
> > End Function
> > --
> > Joe Fallon
> >
> >
> >
> >
> > "Guadala Harry" <> wrote in message
> > news:e6Oe0OB%...
> > > 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.
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Guadala Harry
Guest
Posts: n/a
 
      02-21-2004
Yes - that helps.
You've saved me a bunch of time.
Thanks.

G

"Joe Fallon" <> wrote in message
news:uHXwLBL%...
> 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" <> wrote in message
> news:O8$eq1E%...
> > 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" <> wrote in message
> > news:%23bcsaRC%...
> > > 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(ByVal commandText As

> > String)
> > > As IDataReader
> > > Return ExecuteReader(mConnStr, CommandType.Text, commandText,
> > > CType(Nothing, IDataParameter()))
> > > End Function
> > >
> > > Public Overloads Shared Function ExecuteReader(ByVal spName As String,

> > ByVal
> > > ParamArray parameterValues() As Object) As IDataReader
> > > Return ExecuteReader(mConnStr, spName,

> CommandType.StoredProcedure,
> > > parameterValues)
> > > End Function
> > >
> > > Public Overloads Shared Function ExecuteReader(ByVal commandType As
> > > CommandType, ByVal commandText As String) As IDataReader
> > > Return ExecuteReader(mConnStr, commandType, commandText,
> > > CType(Nothing, IDataParameter()))
> > > End Function
> > > ===========================
> > > All calls eventually end up here:
> > >
> > > Private Overloads Shared Function ExecuteReader(ByVal connection As
> > > IDbConnection, ByVal transaction As IDbTransaction, ByVal commandType

As
> > > CommandType, ByVal commandText As String, ByVal commandParameters() As
> > > IDataParameter, ByVal connectionOwnership As ConnectionOwnership) As
> > > IDataReader
> > > If (connection Is Nothing) Then Throw New
> > > ArgumentNullException("Missing connection")
> > > Dim cmd As IDbCommand = CreateCommand()
> > > Dim dr As IDataReader
> > > Dim mustCloseConnection As Boolean = False
> > >
> > > Try
> > > PrepareCommand(cmd, connection, transaction, commandType,
> > > commandText, commandParameters, mustCloseConnection)
> > > If connectionOwnership = connectionOwnership.External Then
> > > dr = cmd.ExecuteReader()
> > > Else
> > > dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
> > > End If
> > >
> > > 'Detach the Parameters from the command object, so they can be

> > used
> > > again
> > > Dim canClear As Boolean = True
> > > Dim commandParameter As IDataParameter
> > > For Each commandParameter In cmd.Parameters
> > > If commandParameter.Direction <> ParameterDirection.Input

Then
> > > canClear = False
> > > End If
> > > Next
> > >
> > > If (canClear) Then cmd.Parameters.Clear()
> > >
> > > Return dr
> > > Catch
> > > If (mustCloseConnection) Then connection.Close()
> > > Throw
> > > End Try
> > > End Function
> > > --
> > > Joe Fallon
> > >
> > >
> > >
> > >
> > > "Guadala Harry" <> wrote in message
> > > news:e6Oe0OB%...
> > > > 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.
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Middle Tier for Java and PHP John Java 7 04-07-2006 03:26 PM
Setting up a new tier in a 3-tier system? Steve Kershaw ASP .Net 3 03-29-2006 01:09 AM
nTier question regarding DataSets and DataReaders Ryan Ternier ASP .Net 5 11-10-2004 09:48 PM
2 tier to 3 tier? NOSPAM ASP .Net 1 10-14-2004 10:51 PM
ASP v2 & 3-tier or 2-tier rob ASP .Net 1 08-13-2004 06:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57