-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Sitaraman
Message 6 in Discussion

Hi Rajesh.   That was a good explanation, but a few corrections...      " you will 
find that the constructor for DataReader is private " : The Access modifier of the 
constructor of a DataReader is not "Private" but  "Friend"(internal in C#). Actually 
the signature of the contructor of a DataReader is as follows

VB.Net
Friend Sub New(ByVal command As SqlCommand)
End Sub

C#
internal SqlDataReader(SqlCommand command)
{
}

Friend(internal in C#) Access Modifier for the element basically means that "elements 
are accessible from within the program that contains their declaration and from 
anywhere else in the same assembly"
        " when you call a public method ExecuteReader of DataReader" :  ExecuteReader 
is a method of the SQLCommand and not the DataReader.  Note that in the previous 
point, i had mentioned that the constructor of the DataReader has a "Friend" 
Specifier. This means tht whereas you cannot use a new for DataReader due to the 
"Friend"  access specifier, the SQLCommand  can very well use a "new DataReader()", as 
SQLCommand is in the same assembly as that of SQLDataReader.
        "This is infact an example of singleton pattern" : This is not a Singleton in 
any way. Singleton is basically a mechanism where "you make an instance of an object 
globally available and guarantee that only one instance of the class is created".  
DataReader is no way Singleton, neither by design nor by implementation.  The way a 
Singleton is implemented is as follows
<MSDN_Snip>
             1.  Making the class create a single instance of itself. 
             2. Allowing other objects to access this instance through a class method 
that returns a reference to the instance. A class method is globally 
             3. accessible. 
             Declaring the class constructor as private so that no other object can 
create a new instance. 
</MSDN_Snip>
Lets see whether the DataReader conforms to the afore -mentioned points
             1. It doesnt conform : You can have multiple instances of DataReaders.
             2.  It doesnt conform : A Class method that returns an instance of its 
own is nowhere provided in DataReader.  The method for returning an 
             instance of a  DataReader is actually provided by  another class, i.e. 
SQLCommandand there is no concept of Single instance as such
             3.  It doesnt conform : DataReader Class Constructor is not "Private", 
but a Friend. So other objects, provided they are in the same 
             assembly CAN create an instance of the DataReader.

So DataReader was never meant to be and is NOT a Singleton. To verify this, Create 2 
DataReader Objects, populate them thru SQLCommand.executereader and compare them using 
the Object.ReferenceEquals method.  If DataReaders had been Singleton,  it should 
return a true.  But on using ReferenceEquals u will find that it returns a false, 
whcih means there are multiple instances!!! 
So the reason why you cannot use a new for a DataReader is because of the fact that 
its constructor has a "Friend" Access modifier.  When u want a DataReader populated u 
use the SQLCommand's ExecuteReader method which will perform certain operations(not 
going into the details here) and create a new instance  of the DataReader internally 
and return it to u. So every call to the SQLCommand.executereader will result in one 
new instance of SQLDataReader object.

hth

regards, 
sr 


 

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to