Solved

Why there is no New constructor for dataReader?

Posted on 2014-10-02
3
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

724 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