views:

718

answers:

6

Hi All,

I'm thinking in this line of code

IDataReader myReader = questDatabase.ExecuteReader(getQuest);

I'm using DAAB but I can't understand how and what's the meaning of the fact the method ExecuteReader(DbCommand) returns an IDataReader Interface.

Anyone can Explain, Please

+4  A: 

It returns an interface because the implementation of the interface isn't important, just the API that the interface provides.

David Grant
I Really Thank You ALL, I'm surprising in your dedication, It's grat to be in a comunity like this.Thanks All
netseng
+2  A: 

"Returns an interface" really means: "Returns an instance of some class that implements that interface"

In this case, it returns a object very similar to a SqlDataReader object, that lets you to execute methods like .Read() and implements the IDisposable and IDataRecord interfaces.

Jader Dias
I Really Thank You ALL, I'm surprising in your dedication, It's grat to be in a comunity like this.Thanks All
netseng
+2  A: 

It's not returning an Interface per se, but instead an object that supports that interface.

Rowland Shaw
I Really Thank You ALL, I'm surprising in your dedication, It's grat to be in a comunity like this.Thanks All
netseng
+5  A: 

The method will return an object, which is an instance of a class, and that type of class will support IDataReader.

The point is, the type of the object isn't important, just the fact that the class implements the interface.

If you're driving a car, you don't need to know if it's a ford, or a toyota, you drive the car the same way.

The driving "interface" is the same, once the car supports the interface, you can drive it.

Ditto with the IDataReader, once the class thats returned supports the interface, you can use it.

Binary Worrier
Thanks All, It's great to be in a comunity like this
netseng
+2  A: 

It returns an object that implements this particular interface, and that's all you really care about. The object "is a" IDataReader, and can perform all the methods IDataReader has.

random
I Really Thank You ALL, I'm surprising in your dedication, It's grat to be in a comunity like this.Thanks All
netseng
That's what I like about StackOverflow :p
random
+6  A: 

It allows you to you DataReader without the need of knowing which type of DataReader you are using (i.e. SqlDataReader, OleDbDataReader, EtcDataReader), so if someday you want to change the datareader you are using it won't impact you logic, in other words it gives you abstraction. For example :

you can use

IDbCommand command = GiveMeSomeCommand();
IDataReader r = command.ExecuteReader();

without knowing which provider you are using

it can be:

private static IDbCommand GiveMeSomeCommand()
{
    return new OleDbCommand();
}

or it can be

private static IDbCommand GiveMeSomeCommand()
{
    return new SqlCommand();
}

or whatever.

EDIT:

You can also use the DBFactories.

DbProviderFactory factory = GiveMeSomeFactory();
IDbCommand command = factory.CreateCommand();
IDataReader r = command.ExecuteReader();

//and create more objects
IDataAdapter adapter = factory.CreateDataAdapter();
IDbConnection conn = factory.CreateConnection();

and then create your provider in other layer

private DbProviderFactory GiveMeSomeFactory()
{
    if(something)
        return SqlClientFactory.Instance;
    else if(somethingElse)
        return OracleFactory.Instance;
    else if(notThisAndNotThat)
        return MySqlFactory.Instance;
    else
        return WhateverFactory.Instance;

}
pablito
I Really Thank You ALL, I'm surprising in your dedication, It's grat to be in a comunity like this.Thanks All
netseng