This shouldnt be hard to answer but I am so desperate and confused. I made interface for executing read query from database
interface IDatabase
{
DataTable ExecuteReaderCommand(IDbCommand command);
IDbCommand GetNewCommand();
}
Next, I created two different classes implemetnig above interface.
class MysqlDatabase : IDatabase
{
public DataTable ExecuteReaderCommand(MySqlCommand command)
{
DataTable dt = new DataTable();
// ... read db
return dt;
}
public MySqlCommand GetNewCommand()
{
cnn.Open();
return cnn.CreateCommand();
}
}
And
class SQLiteDatabase : IDatabase
{
String dbConnection;
SQLiteConnection cnn;
public DataTable ExecuteReaderCommand(SQLiteCommand command)
{
DataTable dt = new DataTable();
// ... read db
return dt;
}
public SQLiteCommand GetNewCommand()
{
cnn.Open();
return cnn.CreateCommand();
}
}
But I am getting error that these classes dont implement IDatabase interface:
MysqlDatabase does not implement interface member 'Database.GetNewCommand()'
MysqlDatabase.GetNewCommand() cannot implement 'Database.GetNewCommand()' because it does not have the matching return type of 'System.Data.IDbCommand'.
SQLiteDatabase does not implement interface member Database.ExecuteReaderCommand(System.Data.IDbCommand)
SQLiteDatabase does not implement interface member 'Database.GetNewCommand()'. SQLiteDatabase.GetNewCommand() cannot implement Database.GetNewCommand() because it does not have the matching return type of 'System.Data.IDbCommand'.
When I look on the SQLiteCommand and MySqlCommand they both implements IDbCommand.
How can I use these classes under common interface so I can easilly switch them?
I am very thankful for any answer.
No they don’t. They claim they do, but they don’t actually provide the right methods. Look at
IDatabase.GetNewCommand():and your implementation:
They’ve got different return types. Likewise your
ExecuteReaderCommandmethod parameter isIDbCommandinIDatabase, butMySqlCommandinMysqlDatabase.Options:
Use explicit interface implementation for the “weakly typed” version, exposing the “strongly typed” versions on the concrete classes. This is what
SqlCommanddoes in the .NET framework, for example.Make
IDatabasegeneric in the type of command it creates and uses: