In this chapter, we will cover the following recipes:
The process of configuring NHibernate is fairly intensive and takes time. NHibernate has to load, parse, and compile all our mappings and reflect the model. In this recipe, we'll show you how to reduce the start up time of your NHibernate application.
Complete the Configuring NHibernate with App.config
recipe discussed in the Chapter 1, The Configuration and Schema.
System.Configuration.dll
.ConfigurationBuilder
:using System; using System.Configuration; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using Configuration = NHibernate.Cfg.Configuration; namespace ConfigByAppConfig { public class ConfigurationBuilder { private const string SERIALIZED_CFG = "configuration.bin"; } }
Build
with the following code:public Configuration Build() { Configuration cfg = LoadConfigurationFromFile(); if (cfg == null) { cfg = new Configuration().Configure(); SaveConfigurationToFile(cfg); } return cfg; }
LoadConfigurationFromFile
with the following code:private Configuration LoadConfigurationFromFile() { if (!IsConfigurationFileValid()) return null; try { using (var file = File.Open(SERIALIZED_CFG, FileMode.Open)) { var bf = new BinaryFormatter(); return bf.Deserialize(file) as Configuration; } } catch (Exception) { // Something went wrong // Just build a new one return null; } }
IsConfigurationFileValid
with the following code:private bool IsConfigurationFileValid() { // If we don't have a cached config, // force a new one to be built if (!File.Exists(SERIALIZED_CFG)) return false; var configInfo = new FileInfo(SERIALIZED_CFG); var asm = Assembly.GetExecutingAssembly(); if (asm.Location == null) return false; // If the assembly is newer, // the serialized config is stale var asmInfo = new FileInfo(asm.Location); if (asmInfo.LastWriteTime > configInfo.LastWriteTime) return false; // If the app.config is newer, // the serialized config is stale var appDomain = AppDomain.CurrentDomain; var appConfigPath = appDomain.SetupInformation.ConfigurationFile; var appConfigInfo = new FileInfo(appConfigPath); if (appConfigInfo.LastWriteTime > configInfo.LastWriteTime) return false; // It's still fresh return true; }
SaveConfigurationToFile
with this code:private void SaveConfigurationToFile(Configuration cfg) { using (var file = File.Open(SERIALIZED_CFG, FileMode.Create)) { var bf = new BinaryFormatter(); Improving Performance bf.Serialize(file, cfg); } }
Program.cs
, replace the NHibernate configuration code with the following code:var nhConfig = new ConfigurationBuilder().Build();
Validating the mappings and settings thoroughly takes time and effort. We can't escape this when our application runs for the very first time; however, if we serialize our Configuration
object to disk, we can deserialize it the next time we run it, thus saving us all the work.
The IsConfigurationFileValid
method ensures that the configuration we've serialized is still fresh. We need to rebuild our Configuration
object from scratch once the executable or the App.config
has been updated.
We compare the last write-time of the various files to check whether the serialized configuration is stale. We use a BinaryFormatter
to serialize and deserialize the configuration.
This technique is especially suited for development and test suites, where we frequently change code, but may not change our mappings or configuration. We can skip all the extra parsing and get running quickly and test our changes.
It also works well for desktop NHibernate applications. Since a user is waiting for your application to launch, every second counts. It's not just as useful for web applications in production because they basically launch once and stay running.
18.118.226.109