Thursday 23 September 2010

Extension method for adding export hbm from fluent nhibernate when in a debug environment

Came up with a useful little extension method today. I wanted the fluent nhibernate configuration to export the hbm files when the site is being run in debug mode.

   1: /// <summary>
   2: /// Export hbm to path if debug environment
   3: /// </summary>
   4: /// <param name="fluentMappingsContainer">Container</param>
   5: /// <param name="path">Path</param>
   6: /// <returns>container</returns>
   7: public static FluentMappingsContainer ExportPathIfDebug(this FluentMappingsContainer fluentMappingsContainer, string path)
   8: {
   9:     #if DEBUG            
  10:     if (ConfigurationManager.AppSettings[path] != null)
  11:     {
  12:         if (!Directory.Exists(ConfigurationManager.AppSettings[path])) Directory.CreateDirectory(ConfigurationManager.AppSettings[path]);
  13:         fluentMappingsContainer.ExportTo(ConfigurationManager.AppSettings[path]);
  14:     }
  15:     #endif
  16:     return fluentMappingsContainer;
  17: }

Where the path refers to an application setting in the app.config.