On of the most important thing when you want to start with NHibernate is to know how make NHibernate configuration. I will show you using normal approach and Fluent NHibernate.
1. Configuration properties in App.config / Web.config
You need to create all NHibernate configuration properties in App.config or Web.config. This is an example of how to specify the database connection properties inside a config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=(local);database=dbase;Integrated Security=SSPI; </property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
<!-- other app specific config follows... -->
</configuration>
2. Configuration properties in hibernate.cfg.xml
You can also configure NHibernate configuration properties in hibernate.cfg.xml and this file need to save into bin directory otherwise NHibernate claim it’s not found the resource file.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="UnitTest">
<property name="connection.connection_string_name">Server=(local);initial catalog=Test;user id=sa;password=qwe123</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="show_sql">true</property>
<property name="connection.release_mode">auto</property>
<property name="adonet.batch_size">500</property>
</session-factory>
</hibernate-configuration>
Time to test.
I will use nunit to test that nhibernate can read this config file
Configuration cfg;
[TestFixtureSetUp]
public void ConfigureUsingNormalApproach()
{
cfg = new Configuration().Configure();
cfg.AddAssembly(Assembly.Load("ScrumDev.Domain"));
}
You should get the test result in green color.
0 comments:
Post a Comment