Implementing a Generic Database Interface in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,



I am beginner in C#.
I am currently migrating a system which was written in VB .NET to C#.
In the VB program, the programmer is able to choose the DBMS between SQL Server 2000 and Oracle,
according to a configuration string stored on the Web.config file.
This way, when the programmer wants to instantiate a data reader for example, he/she uses an
interface to do the job as in the code snippet below.
 
Hi,
The big problem is that I cannot have a method that has not a declared
datatype like in VB.

You are returining an object - just create a method that returns an object.
Anyway, your method could/should return IDataReader interface and not an
object...
 
Hi Amintas Lopes Neto,

Amintas Lopes Neto said:
Hi all,



I am beginner in C#.
I am currently migrating a system which was written in VB .NET to C#.
In the VB program, the programmer is able to choose the DBMS between SQL Server 2000 and Oracle,
according to a configuration string stored on the Web.config file.
This way, when the programmer wants to instantiate a data reader for example, he/she uses an
interface to do the job as in the code snippet below.
<snip>

When you declare a function (in VB) like this:

Public Function CreateDataReader()

It translates it to:

Public Function CreateDataReader() As Object

That means the equivalent C# declaration would be:

public object CreateDataReader()

Of course, I would strongly recommend you declare it as:

public IDataReader CreateDataReader()

This way it's self-documenting and you aren't going to accidentally
return something the caller doesn't expect.

Regards,
Dan
 
Amintas Lopes Neto said:
Please notice that it is not mandatory to declare
the return type for the CreateDataReader() method
in VB .NET [...]
Very well, I tried to do the same in C#.
The big problem is that I cannot have a method that
has not a declared datatype like in VB.
Does anybody knows a way to work around it ?

The two types of DataReader will probably have the same public properties
and methods, so it might be an idea to have an abstract base class (or
interface) representing a DataReader, and create two subclasses that inherit
from the base class (or implement the interface). Your return type would
then be the type of the base class or interface.

P.
 
Back
Top