Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about return value of function in an abstract class

Status
Not open for further replies.

CosmicCharlie

Programmer
Jun 30, 2006
44
0
0
US
Hello,

I am designing a data tier and I'm trying to implement it using the Abstract Factory design pattern. I want to allow clients to get data as either a DataReader and DataTable as they see fit.

All is well there. However, as I declare the functions that actually return these objects, I can specify a DataTable return value, but the DataReader return value is not available. Instead, I use the IDataReader interface.

Of course, it all comes out in the client code just fine, and I get the DataReader I want. But I am mystified as to why I can declare a DataTable object return value, but for DataReader I must refer to its interface. (IDataTable, by the way, is NOT available.)

Can anyone shed light on this? The simplified code snippet below illustrates my question.


'*************************************************
Public MustInherit Class dalDataReader
Public MustOverride Property ReturnedDataReader() As IDataReader
End Class

Public MustInherit Class dalDataTable
Public MustOverride Property ReturnedDataTable() As DataTable
End Class
'*************************************************

Charlie
 
This is CosmicCharlie replying to his own question.

Later on I discovered why this is the case. DataTable is in the System.Data namespace, as is IDataReader. DataReader is in the System.Data.SqlClient namespace. I could easily change the property declaration to

Public MustOverride Property ReturnedDataReader() As SqlClient.SqlDataReader

and fix the code to allow this, but then this gets at the heart of why that is a bad idea. This abstract class is supposed to support a DataReader regardless of the data access provider used. To declare it this way would limit it to SQL Server DataReaders, and I don't want to do that.

Since a DataTable is not specific to one data access provider, declaring the other function specifically as a DataTable does not violate the flexibility I am attempting to achieve with this class.

If anyone would like to add a comment, I welcome it.

Charlie

 
That's very interesting and something that I haven't thought of. I'm going to have to do some testing on this.

Thanks for the info!!
 
Yes, if you use any of the concrete database classes (Sql, OracleClient, OleDb), you're tying yourself to that database.

But if you move up in the inheritance/implementation tree to something like the IDataReader, then you can work with any of the database providers.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top