Listato 3. La classe "Manager": DatabaseManager.cs

namespace PeppeDotNet.it.DAL.AbstractFactoryPattern
{
  public sealed class DatabaseManager : IDisposable
  {
    public DatabaseManager(Provider provider)
    {
      this.provider = provider;
      switch (provider)
      {
        case Provider.SQLServer:
          database = new SqlServer();
          break;
        case Provider.Access:
          database = new Access();
          break;
        default:
          break;
      }
    }
		
    private Provider provider;
    private IDbConnection connection;
    private IDataReader reader;
    private IDbCommand command;
    private Database database = null;

    public Provider Provider { get { return provider; } set { provider = value; } }
    public string ConnectionString { get { return ConnectionHelper.GetConnectionString(); } }
    public string ProviderName { get { return ConnectionHelper.GetProviderName(); } }
    public IDbConnection Connection { get { return connection; } }
    public IDataReader Reader { get { return reader; } }

    public void Open()
    {
      connection = database.CreateConnection(ConnectionString);
      if (connection.State != ConnectionState.Open) connection.Open();
      command = database.CreateCommand();
    }

    public IDataReader ExecuteReader(string query)
    {
      command = database.CreateCommand(query, Connection);
      reader = command.ExecuteReader();
      return reader;
    }

    public DataSet ExecuteDataSet(string query)
    {
      command = database.CreateCommand(query, Connection);
      IDbDataAdapter adapter = database.CreateDataAdapter();
      adapter.SelectCommand = command;
      DataSet ds = new DataSet();
      adapter.Fill(ds);

      return ds;
    }

    public object ExecuteScalar(string query)
    {
      command = database.CreateCommand(query, Connection);
      object ritorno = command.ExecuteScalar();
      return ritorno;
    }

    public int ExecuteNonQuery(CommandType commandType, string query)
    {
      command = database.CreateCommand(query, Connection);
      int ritorno = command.ExecuteNonQuery();
      return ritorno;
    }

    public void CloseReader()
    { 
      if (reader != null) reader.Close(); 
    }

    public void Close()
    {
      if (connection.State != ConnectionState.Closed)
        connection.Close();
    }

    public void Dispose()
    {
      CloseReader();
      Close();
      reader.Dispose();
      command.Dispose();
      connection.Dispose();
      connection = null;
      command = null;
    }
  }
}