6.3. An Example BaseDao

In case you are still wondering what the IDao interface looks like in code, here it is:

using System;

namespace IBatisNet.DataAccess.Interfaces {
 public interface IDao {
 
 }
}

It may not look like much, but that's the point! The iBATIS DAO framework uses this interface to interact with your application DAOs since they are marked as implementing it. As mentioned earlier, you don't have to force your DAO interfaces to inherit IDao if you create a class like BaseDao.

using System;
using IBatisNet.Common;
using IBatisNet.DataAccess; //DaoManager, DaoSession
using IBatisNet.DataAccess.Interfaces; //IDao

namespace IBatisNet.Test.Implementations {
 public class BaseDao: IDao {
  protected DaoSession GetContext() {
   IDaoManager daoManager = DaoManager.GetInstance(this);
   return (daoManager.LocalDaoSession as DaoSession);
  }
 }
}

The BaseDao class implements the IDao marker interface and defines a protected GetContext method that will be used by DAO classes that inherit from BaseDao. By calling GetContext, a DAO class gets a DaoSession that is managed by the framework's DaoManager. The DaoSession abstract class uses the IDalSession interface to provide an API to manage transactions and to access ADO.NET objects during the session.

namespace IBatisNet.DataAccess {
 public abstract class DaoSession : IDalSession {

  // Constructor
  public DaoSession(DaoManager daoManager)

  // Properties
  public abstract DataSource DataSource
  public abstract IDbConnection Connection
  public abstract IDbTransaction Transaction
  
  // Methods

  // Complete (commit) a transaction
  public abstract void Complete();

  // Open a database connection.
  public abstract void OpenConnection();

  // Close the connection
  public abstract void CloseConnection();

  // Begin a transaction.
  public abstract void BeginTransaction();

  // Begin a database transaction
  public abstract void BeginTransaction(bool openConnection);

  // Begin a transaction at the data source 
  // with the specified IsolationLevel value.
  public abstract void BeginTransaction(IsolationLevel isolationLevel);

  // Begin a transaction on the current connection
  // with the specified IsolationLevel value.
  public abstract void BeginTransaction(bool openConnection, IsolationLevel isolationLevel);

  // Commit the database transaction.
  // and close the connection.
  public abstract void CommitTransaction();

  // Commit the database transaction.
  public abstract void CommitTransaction(bool closeConnection);

  // Rollback a transaction from a pending state
  // and close the connection.
  public abstract void RollBackTransaction();

  // Rolls back a transaction from a pending state.
  public abstract void RollBackTransaction(bool closeConnection);

  public abstract IDbCommand CreateCommand(CommandType commandType);
  public abstract IDataParameter CreateDataParameter();
  public abstract IDbDataAdapter CreateDataAdapter();
  public abstract IDbDataAdapter CreateDataAdapter(IDbCommand command);
}

The ADO.NET access provided by the DaoSession object gives you the flexibility to sprinkle in ADO-based DAO classes with iBATIS Data Mapper-based DAOs and NHibernate-based DAOs for those special circumstances that often pop up. And you get all this without losing the consistent DAO interface layer or exposing your persistence implementation details.