It is possible to have the .hbm.xml files lying around as files in the application directory. I usually have a subfolder where all the .xml files reside that have the server connection info in them, in that folder there are subfolders with the <connection info filename>\classxyz.hbm.xml files for a particular database. All those files are set to be content and not embedded resource.
that way you can change the hbm.xml files without recompile, just adding new fields obviously won't work ;)
the following is my central function that returns an nhibernate sessionfactory based on the requested database connection file
copy&paste galore
Code:
private ISessionFactory GetSessionFactoryFor(string psessionfactoryconfigpath)
{
Check.Require(!string.IsNullOrEmpty(psessionfactoryconfigpath), "sessionFactoryConfigPath may not be null nor empty");
ISessionFactory lsessionfactory = null;
// Attempt to retrieve a stored SessionFactory from the hashtable containing all factories keyed with their config filename.
// thread safety
lock (sessionFactories)
{
lsessionfactory = (ISessionFactory)sessionFactories[psessionfactoryconfigpath];
// Failed to find a matching SessionFactory so make a new one.
if (lsessionfactory == null)
{
Check.Require(File.Exists(psessionfactoryconfigpath), "The config file at '" + psessionfactoryconfigpath + "' could not be found");
// first create the nhibernate config
NHibernate.Cfg.Configuration lconfiguration = new NHibernate.Cfg.Configuration();
lconfiguration.Configure(psessionfactoryconfigpath);
// now get every hbm.xml file that resides inside the directory named like the database config file
string ldomainobjectbasepath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(psessionfactoryconfigpath), System.IO.Path.GetFileNameWithoutExtension(psessionfactoryconfigpath));
string[] ldomainobjects = System.IO.Directory.GetFiles(ldomainobjectbasepath, "*.hbm.xml");
foreach (string lcurrentdomainobject in ldomainobjects)
{
lconfiguration.AddFile(lcurrentdomainobject);
}
lsessionfactory = lconfiguration.BuildSessionFactory();
if (lsessionfactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
sessionFactories.Add(psessionfactoryconfigpath, lsessionfactory);
}
}
return lsessionfactory;
}