1
resposta

Gostaria de saber se essa implementação usando o padrão Factory para conexão de banco de dados está legal. As classes concretas herdam de Database



using System.Data;

public abstract class Database
{
    public string connectionString;

    public abstract IDbConnection CreateConnection();
    public abstract IDbCommand CreateCommand();
    public abstract IDbConnection CreateOpenConnection();
    public abstract IDbCommand CreateCommand(string commandText, IDbConnection connection);
    public abstract IDbCommand CreateStoredProcCommand(string procName, IDbConnection connection);
    public abstract IDataParameter CreateParameter(string parameterName, object parameterValue);

}

using Oracle.DataAccess.Client;

public class OracleDatabase : Database
{
    public override IDbConnection CreateConnection()
    {
        return new OracleConnection(connectionString);
    }
    public override IDbCommand CreateCommand()
    {
        return new OracleCommand();
    }
    public override IDbConnection CreateOpenConnection()
    {
        OracleConnection connection = (OracleConnection)CreateConnection();
        connection.Open();
        return connection;
    }
    public override IDbCommand CreateCommand(string commandText, IDbConnection connection)
    {
        OracleCommand command = (OracleCommand)CreateCommand();
        command.CommandText = commandText;
        command.Connection = (OracleConnection)connection;
        command.CommandType = CommandType.Text;
        return command;
    }
    public override IDbCommand CreateStoredProcCommand(string procName, IDbConnection connection)
    {
        OracleCommand command = (OracleCommand)CreateCommand();
        command.CommandText = procName;
        command.Connection = (OracleConnection)connection;
        command.CommandType = CommandType.StoredProcedure;
        return command;
    }
    public override IDataParameter CreateParameter(string parameterName, object parameterValue)
    {
        return new OracleParameter(parameterName, parameterValue);
    }
}
1 resposta

Opa, pensando pelo lado da factory, acho que ta ok.. vc realmente encapsula códigos que são mais complicados para criação de coisas... O que recebe a connection como argumento eu acho um pouco estranho, já que a connection é criada pela mesma classe... mas nada muito grave.