Saturday, November 6, 2010

NHibernate Hidden Gems - Speed up NHibernate startup time

This code is pulled directly from Ayende's presentation at NHDay in Bologna. I would just link there, but this code only exists in the video (no source was provided).

Here's a simple solution that Ayende showed to speed up the NHibernate startup time. The following will work great for production systems. You would need something a little more complex for development since you would need to re-save the cfg file anytime you change your mappings.

if (!File.Exists("nh.cfg"))
{
// Build your nh configuration
Configuration configuration = new Configuration()
.Configure("NHibernate.config");
// write the config out to a file (the configuration is expensive to create)
new BinaryFormatter().Serialize(File.Create("nh.cfg"), configuration);
}
var configuration = (Configuration)new BinaryFormatter().Deserialize(
File.Open("nh.cfg", FileMode.Open)
);

ISessionFactory factory = configuration.BuildSessionFactory();

0 comments:

Post a Comment