Solved

Why there is no New constructor for dataReader?

Posted on 2014-10-02
3
176 Views
Last Modified: 2014-10-05
This is a part of code I have:

SqlDataReader dr = cmd.ExecuteReader();

If dr is an object and SqlDataReader is a class? What the above code is doing?

Is it instantiating? Or, it already has been instantiated so we are just initializing it.

I appreciate for posing your comment explaining how this is working?
0
Comment
Question by:Mike Eghtebas
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 350 total points
ID: 40358378
There is one, every class has a constructor.

But if you look at the documentation for the basic DBDataReader (not a more specific one such as the SqlDataReader or the OleDBDataReader), it is defined as protected instead of public as most constructors are.

That means that only classes that inherits from the DataReader can call it. So it is called by the SqlDataReader (or any of the other data readers) because this one inherits from it, but you can't.

On their side classes such as the SqlDataReader have alternate constructors that are declared Friend (in VB) / internal (in C#). These types of methods can be called only internally inside of a project, inside of the System.Data.dll for the SqlDataReader.

When you call ExecuteReader, because that method is in the same dll, it can call the internal SqlDataReader, which in turn can call the protected DBDataReader that offers the base functionalities of a Data Reader. It passes back the created DataReader back to you as the return value of the ExecuteReader method.

I have not seen documentation from Microsoft that states why they designed the data reader mechanism that way, but this is often done for security reasons. I have seen such a mechanism used in a situation where a few things had to be set up before the class could be instantiated. Instead of requiring the user to write a lot of stuff every time he needed to create a new object (which goes against the ideas of object oriented programming), the programmer did something similar what Microsoft did with the data reader: call a method that will both do the proper initialization, instantiate the object and return it to you.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40358410
Thanks,

Mike
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 40358918
These types of methods can be called only internally inside of a project, inside of the System.Data.dll for the SqlDataReader.
That is, unless InternalsVisibleToAttribute is used to expose internal members to specific assemblies--which, of course, System.Data would not be exposing anything to your code in this fashion!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Graphics 2 28
How do you do a one to many list in .NET CORE? 2 27
Get number of Files in Directory and Sub Directories 2 41
What Does This C# Code Block Do? 5 30
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question