Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can improve the startup time (of both web and windows applications) by caching the Configurations. The following class will do this job:</p> <pre><code>using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Web; using NHibernate.Cfg; namespace NH32AutoMap.Core { public class ConfigurationFileCache { private readonly string _cacheFile; private readonly Assembly _definitionsAssembly; public ConfigurationFileCache(Assembly definitionsAssembly) { _definitionsAssembly = definitionsAssembly; _cacheFile = "nh.cfg"; if (HttpContext.Current != null) //for the web apps _cacheFile = HttpContext.Current.Server.MapPath( string.Format("~/App_Data/{0}", _cacheFile) ); } public void DeleteCacheFile() { if (File.Exists(_cacheFile)) File.Delete(_cacheFile); } public bool IsConfigurationFileValid { get { if (!File.Exists(_cacheFile)) return false; var configInfo = new FileInfo(_cacheFile); var asmInfo = new FileInfo(_definitionsAssembly.Location); if (configInfo.Length &lt; 5 * 1024) return false; return configInfo.LastWriteTime &gt;= asmInfo.LastWriteTime; } } public void SaveConfigurationToFile(Configuration configuration) { using (var file = File.Open(_cacheFile, FileMode.Create)) { var bf = new BinaryFormatter(); bf.Serialize(file, configuration); } } public Configuration LoadConfigurationFromFile() { if (!IsConfigurationFileValid) return null; using (var file = File.Open(_cacheFile, FileMode.Open, FileAccess.Read)) { var bf = new BinaryFormatter(); return bf.Deserialize(file) as Configuration; } } } } </code></pre> <p>To use that,</p> <pre><code>private Configuration readConfigFromCacheFileOrBuildIt() { Configuration nhConfigurationCache; var nhCfgCache = new ConfigurationFileCache(MappingsAssembly); var cachedCfg = nhCfgCache.LoadConfigurationFromFile(); if (cachedCfg == null) { nhConfigurationCache = buildConfiguration(); nhCfgCache.SaveConfigurationToFile(nhConfigurationCache); } else { nhConfigurationCache = cachedCfg; } return nhConfigurationCache; } </code></pre> <p>And then before calling the BuildSessionFactory, we can read the config file from cache or if the mappings have changed, build it and cache it again:</p> <pre><code>public ISessionFactory SetUpSessionFactory() { var config = readConfigFromCacheFileOrBuildIt(); var sessionFactory = config.BuildSessionFactory(); </code></pre> <p>Here you can find a full sample: (<a href="https://dotnettipsrepository.svn.codeplex.com/svn/Trunk/ORMs/NHibernate/NH32AutoMap.zip" rel="noreferrer">^</a>). + If you want to make it work, separate domain classes and mappings definitions assemblies from the main application's assembly (because the ConfigurationFileCache class will delete the cache file if the mappings definitions assembly is newer than the cache file's LastWriteTime).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload