Using Interfaces

J

J L

I am still struggling to understand the usage of Interfaces in an
attempt to make a provider independent data access layer. My question
is this...if I create a connection from an interface that is specfic
to a data prvoider, will the remaining objects created from interfaces
and based on the connection object automatically be of the same
provider type?

For example, does this work....

(Note: strConnect and strSql set elsewhere based on the
desired dbType and action)

Dim conn As IDbConnection
Dim cmd As IDbCommand
Dim da as IDbDataAdapter
dim ds as DataSet

Select Case dbType
Case "SQL"
conn = New SQLCommand(strConnect)
Case "OleDB"
conn = New OleDBCommand(strconnect)
End Select

cmd = New IDbCommand(strSql, conn)
da = New IDbDataAdapter(cmd)
da.Fill(ds, "SomeTable")


TIA,
John
 
S

Sahil Malik

John,

An interface at it's very basic is a common contract. Thats it.
In other words, it is not supposed to contain any implementation inside it.

So if I have two different cars to be represented, I could define an
interface called ICar. And ICar enforces that it must have an engine. So
every car must have an engine.

So
Ford implements ICar
Toyota implements ICar

... and now you know that any car your code will ever encounter, can rely on
the common minimum functionality identified by ICar and be able to use the
Car. So if after you ship your code, a Honda Implements ICars shows up, your
code will still work.

IDbConnection is the same way. All it says is "Hey to qualify as a valid
ado.net connection, you must implement this interface" (.NET 2.0 includes
DbConnection that you must inherit from too, but lets not confuse the main
issue).

So think of it from that point of view, IDbConnection qualifies you to be a
connection, now the exact implementation of "Open", "Close" etc. is left to
the implementor.

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
 
J

J L

Hi Sahil,
Thank you for your response. I did understand that part about
interfaces. But I am not clear about what happens when other
interfaces use an object created from a specific provider type.

To use your car analogy...
UKCar implements ICar
USCar Implements ICar

ISterringWheel is passed a car object when created:

Dim myCar As New USCar
Dim mySteeringWheel as ISteeringWheel

mySteeringWheel = New ISteeringWheel(myCar)

will the property mySteeringWheel.IsOnWhatSide be "Left" as expected?

Thanks again,
John
 
S

Sahil Malik

John,

Such code will give you an error -

Cannot create an instance of the abstract class or interface

So there is no steering wheel until you implement a definition for it, in a
class that implements ICar. Then you can define where the steering wheel is.

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top