Using app.config or web.config is the easiest way to configure NHibernate, but is not the only way.
The Configuration constructor configures NH from the app.config or web.config file, if it exists. After constructing the object, you can call the Configure() method to overwrite the initial values. This has four overloads, which can configure NH from an XML file in the file system, from a resource in an assembly or from an XmlTextReader. The following code will build two session factories from two different config files.
Code:
SessionFactory factory1 = new Configuration()
.Configure(configFile1)
.BuildSessionFactory();
SessionFactory factory2 = new Configuration()
.Configure(configFile2)
.BuildSessionFactory();
The following is based on a config file from the NHibernate test suite
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory name="NHibernate.Test">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=localhost;initial catalog=nhibernate;User Id=;Password=</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">444</property>
<property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
<mapping file="ABC.hbm.xml" />
<mapping resource="NHibernate.DomainModel.Simple.hbm.xml" assembly="NHibernate.DomainModel" />
</session-factory>
</hibernate-configuration>
You can also configure NH programatically using the Properties collection of the Configuration object and various methods to add mappings from an assembly, xml file, resource, etc.