XML configuration

XML configuration, such as XML mapping is the first and original method of configuring NH. In this method, all configuration options are declared through XML elements inside either application configuration file or a separate file having extension .cfg.xml. A config section that is handled by NHibernate.Cfg.ConfigurationSectionHandler needs to be added to application configuration for NHibernate configuration to be processed successfully. All configuration options we discussed at the beginning of the chapter are declared using XML as follows:

<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration- 2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
      <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
      <property name="connection.connection_string">Data Source=:memory: </property>
      <property name="show_sql">true</property>
      <mapping resource="Persistence.Mappings.Xml.Employee.hbm.xml" assembly="Persistence"/>
      <mapping resource="Persistence.Mappings.Xml.Address.hbm.xml" assembly="Persistence"/>
      <mapping resource="Persistence.Mappings.Xml.Benefit.subclass.hbm.xml" assembly="Persistence"/>
      <mapping resource="Persistence.Mappings.Xml.Community.hbm.xml" assembly="Persistence"/>
    </session-factory> 
  </hibernate-configuration>
</configuration>

XML configuration is not very difficult to understand. For starters, you need to define a config section named hibernate-configuration. Next to that, you have got actual declaration of hibernate-configuration as top level XML element. Inside this element, you have got the session-factory element. It is inside this element that all options are configured. As you can see, there are mainly two types of options configured here. One is a set of properties configured using the property element. Second is mappings configured using the mapping element.

There is a name attribute on the property element which is used to declare name of the property. The value inside the element is value of the property.

Mappings need a resource and assembly declared. Assembly is obvious. Resource is a fully qualified name of the XML mapping file. Fully qualified name works like as namespace for classes. In our case, the XML mapping files are in the Persistence project in folder Mappings/Xml, hence the fully qualified name would be Persistence.Mappings.Xml.<mapping file name>.

After XML configuration is declared in application configuration file, NHibernate needs to be told about it. You do this by executing the following lines of code from a convenient place in your application:

var config = new Configuration();
config.Configure();

By now, you know that configuration object needs to be initialized only once. So make sure you execute the preceding code such that no multiple instances of configuration objects are created. This can be achieved by placing the preceding code in static constructor of a helper class or inside a singleton instance of a class.

As with XML mappings, XML configuration has lived its age in a way. With success of FNH and large number of enhancements being made to programmatic configuration recently, more and more people seem inclined towards using programmatic configuration or fluent configuration. I have covered XML configuration for the sake of completeness here but we will not be using it in rest of the book. We will mostly be using either programmatic or fluent configuration. Having said that, let me also tell you that you can combine XML based configuration with the programmatic configuration if you want. Part of configuration that needs to change from time to time can be kept in XML - while the part that does not change can be configured programmatically. If you want to use programmatic configuration completely then you can keep the changing parts under appSettings inside web.config in case of a web application. Similar option is available for desktop or WPF application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset