I'm doing tests on methods that use database connections and I like using factory patterns, so I'm doing a factory of DbCommands. My application uses a NpgsqlCommand and the tests are going to use SQLiteCommands.
So my factory code looks like this, only the application factory code (NpgCommandFactory) is included:
public interface IDbCommandFactory{
DbCommand Create();
}
class ClassWithMethodToTest{
private IDbCommandFactory DbCommandFactory;
public ClassWithMethodToTest(){
DbCommandFactory = new NpgCommandFactory();
}
private class NpgCommandFactory : IDbCommandFactory{
private string ConnectionString = "Server=server;Port=5432;Database=database;User Id=user;Password=password;";
public DbCommand Create(){
var connection=new NpgsqlConnection(ConnectionString);
connection.Open();
var command = connection.CreateCommand();
command.CommandText=@"
select
name
from
data";
return new NpgsqlCommand(command.CommandText);
}
}
}
I'm having this error:
cannot convert from 'System.Data.Common.DbCommand' to 'Npgsql.NpgsqlCommand'
I thought this would work because NpgsqlCommand class extends the DbCommand class
public sealed class NpgsqlCommand : DbCommand, ICloneable
I've done something similar with Streams, where I returned a S3Stream (class that extends Stream class) and it worked:
public Stream Create(){
return new S3ReadStream(
S3, "bucketname"
);
}
So why when doing the same with DbCommand and NpgsqlCommand doesn't work?
Aucun commentaire:
Enregistrer un commentaire