5.2. Configuration with the DomDaoManagerBuilder

iBATIS offers you a plethora of options for loading your dao.config file, such as loading it through a file path, Stream, Uri, FileInfo, or XmlDocument. All of these methods are available through the DomDaoManagerBuilder API for creating DaoManager instances.

The basic DomDaoManagerBuilder.Configure() call will look for a file named dao.config in your application's root directory. This directory's location differs by project type but is normally the directory where you place your web.config or app.config file.

Example 5.1. Basic Configuration Call

DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure();
IDaoManager daoManager = DaoManager.GetInstance("SqlMapDaoContext");

If you have named your configuration file something other than dao.config or if you have located your configuration file in a directory other than the application root directory, you can also pass in a relative or absolute file path to the Configure method.

Example 5.2. Configuration through an absolute or relative file path

/* Configure from a file path.
   Uses a relative resource path from your application root 
   or an absolute file path such as "file://c:\dir\a.config" */
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(strPath);
IDaoManager daoManager = DaoManager.GetInstance("AnotherContext");

[Tip]Tip

Since the application root directory location differs by project type (Windows, Web, or library), you can use an AppSettings key for defining a relative path to your dao.config file. Having this key defined makes it easy to change the path without having to recompile your code:

builder.Configure(
        ConfigurationSettings.AppSettings["rootPath"]+"dao.config");

Aside from using a simple string filepath, you can also pass in a FileInfo or Uri instance for the DomDaoManagerBuilder to use in locating your dao.config file.

Example 5.3. Configuration with a FileInfo or Uri instance

/* Configure with FileInfo. */
FileInfo aFileInfo = someSupportClass.GetDynamicFileInfo();
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(aFileInfo);
IDaoManager daoManager = DaoManager.GetInstance("NHibernateContext");

/* Configure through a Uri. */
Uri aUri = someSupportClass.GetDynamicUri();
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(aUri);
IDaoManager daoManager = DaoManager.GetInstance("SimpleDao");

If you find that you already have loaded your DAO configuration information as an XmlDocument or Stream instance within your application, the DomDaoManagerBuilder provides Configure overloads for those types as well.

Example 5.4. Configuration with an XmlDocument or Stream

/* Configure with an XmlDocument */
XmlDocument anXmlDoc = someSupportClass.GetDynamicXmlDocument();
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(anXmlDoc);
IDaoManager daoManager = DaoManager.GetInstance("Petstore");

/* Configure from a stream. */
Stream aStream = someSupportClass.GetDynamicStream();
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(aStream);
IDaoManager daoManager = DaoManager.GetInstance("AnotherPetstore");

In addition to the straightforward Configure methods, the DomDaoManagerBuilder provides ConfigureAndWatch methods that can be used to monitor changes to the configuration files so that DaoManagers can be reconfigured and reloaded on the fly. To use this functionality, you application will need to pass a ConfigureHandler (callback delegate) to the DomDaoManagerBuilder so that it knows the method for resetting your application's DaoManager instances.

Since the configuration files need to be watched for changes, your dao.config file must be accessible through the file system. This means that configuration is limited to the three methods shown below.

Example 5.5. DomDaoManagerBuilder ConfigureAndWatch methods

/* Configure and monitor the configuration file for modifications 
   and automatically reconfigure DaoManagers. 
   This basic ConfigureAndWatch method looks for a file with the 
   default name of dao.config in the application root directory. */
public void ConfigureAndWatch(ConfigureHandler configureDelegate)

/* Configure and monitor the configuration file for modifications 
   and automatically reconfigure DaoManagers. 
   Uses a relative path from your application root 
   or an absolute file path such as "file:\\c:\dir\a.config" */
public void ConfigureAndWatch( string resource, ConfigureHandler configureDelegate )

/* Configure and monitor the configuration file for modifications 
   and automatically reconfigure DaoManagers. 
   Uses a FileInfo instance for your config file. */
public void ConfigureAndWatch( FileInfo resource, ConfigureHandler configureDelegate )