returning interfaces

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?
 
Mike said:
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?

Because then the implementation can return an object that looks like a
IDataReader and behaves the way it is supposed to, without actually
having to inherit from DataReader.

Interfaces are useful for making your own classes in your own class
hierarchy pluggable into some other kind of class hierarchy which has
nothing in common with yours.
 
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader.  Why would you want to do this?

*** Sent via Developersdexhttp://www.developersdex.com***

Hi,

Because you do not care about the actual type being returned, you do
not care if it's a SqlDataReader or an OracleDataReader, the only
thing you care about is that it implements the IDataReader interface.
 
It separates the interface from the implementation.

Here is a decent explanation....

http://bytes.com/forum/thread223924.html

-Drew


message
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?

*** Sent via Developersdexhttp://www.developersdex.com***

Hi,

Because you do not care about the actual type being returned, you do
not care if it's a SqlDataReader or an OracleDataReader, the only
thing you care about is that it implements the IDataReader interface.
 
Back
Top