简体   繁体   English

MySql和SQlite类实现接口

[英]MySql and SQlite classes implementing interface

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. 接下来,我在接口上面创建了两个不同的类Implementmetnig。

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: 但我得到错误,这些类不实现IDatabase接口:

 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 . 当我查看SQLiteCommandMySqlCommand它们都实现了IDbCommand

How can I use these classes under common interface so I can easilly switch them? 如何在通用界面下使用这些类,以便我可以轻松切换它们?

I am very thankful for any answer. 我非常感谢任何答案。

When I look on the SQLiteCommand and MySqlCommand they both implements IDbCommand 当我查看SQLiteCommand和MySqlCommand时,它们都实现了IDbCommand

No they don't. 不,他们没有。 They claim they do, but they don't actually provide the right methods. 他们声称他们这样做,但他们实际上并没有提供正确的方法。 Look at IDatabase.GetNewCommand() : 看看IDatabase.GetNewCommand()

IDbCommand GetNewCommand();

and your implementation: 和你的实施:

public MySqlCommand GetNewCommand()
{
    ... 
}

They've got different return types. 他们有不同的回报类型。 Likewise your ExecuteReaderCommand method parameter is IDbCommand in IDatabase , but MySqlCommand in MysqlDatabase . 同样你ExecuteReaderCommand方法参数IDbCommandIDatabase ,但MySqlCommandMysqlDatabase

Options: 选项:

  • Use explicit interface implementation for the "weakly typed" version, exposing the "strongly typed" versions on the concrete classes. 对“弱类型”版本使用显式接口实现,在具体类上公开“强类型”版本。 This is what SqlCommand does in the .NET framework, for example. 例如,这就是SqlCommand在.NET框架中所做的事情。

  • Make IDatabase generic in the type of command it creates and uses: 使IDatabase在其创建和使用的命令类型中具有通用性:

     public interface IDatabase<TCommand> where TCommand : IDbCommand { DataTable ExecuteReaderCommand(TCommand command); TCommand GetNewCommand(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM