471,348 Members | 1,169 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

returning interfaces

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 Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
3 1339
Mike P wrote:
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 Developersdex http://www.developersdex.com ***
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
On Apr 17, 6:56*am, Mike P <mike.p...@gmail.comwrote:
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.
Jun 27 '08 #3
It separates the interface from the implementation.

Here is a decent explanation....

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

-Drew
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:09**********************************@24g2000h sh.googlegroups.com...
On Apr 17, 6:56 am, Mike P <mike.p...@gmail.comwrote:
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.

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by baylor | last post: by
41 posts views Thread by Materialised | last post: by
30 posts views Thread by Frank Rizzo | last post: by
8 posts views Thread by John | last post: by
22 posts views Thread by RSH | last post: by
17 posts views Thread by Cralis | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.