Can't easily do this if there's the possibility of different databases, but I have something for when you have the same database.
Here's a couple of classes...
Code:
/// <summary>
/// Allows us to load mapping assembly information from a config file.
/// </summary>
public class MappingAssemblyConfigHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
/// <summary>
/// Reads the config file to create a MappingAssemblies object
/// </summary>
/// <param name="parent"></param>
/// <param name="configContext"></param>
/// <param name="section"></param>
/// <returns></returns>
public object Create(object parent, object configContext, XmlNode section)
{
MappingAssemblies assemblies = new MappingAssemblies();
foreach (XmlNode node in section.SelectNodes("assembly"))
assemblies.Names.Add(node.SelectSingleNode("@name").Value);
return assemblies;
}
#endregion
}
/// <summary>
/// An collection of assemblies that contains persistence mapping information
/// </summary>
public class MappingAssemblies
{
private static IList names;
public MappingAssemblies()
{
names = new ArrayList();
}
/// <summary>
/// The names of the assemblies
/// </summary>
public IList Names
{
get { return names; }
}
}
Then in your session manager you have a snippet like the following
Code:
/// <summary>
/// Creates the configuration from the config parameters
/// </summary>
/// <returns></returns>
private Configuration InitializeConfiguration()
{
Configuration cfg = new Configuration();
// The following makes sure the the web.config contains a declaration for the persistenceMappingAssemblies config section
MappingAssemblies assemblies =
(MappingAssemblies) ConfigurationManager.GetSection("persistenceMappingAssemblies");
if (assemblies == null)
throw new ConfigurationErrorsException(
"NHibernateSessionManager.InitializeConfiguration: \"persistenceMappingAssemblies\" must be " +
"provided as a section within your config file. \"persistenceMappingAssemblies\" informs NHibernate which assembly(s) " +
"contains the HBM files. It is assumed that the HBM files are embedded resources. An example config " +
"declaration is <persistenceMappingAssemblies><assembly name=\"MyProject.Core\" /></persistenceMappingAssemblies>");
try
{
foreach (string name in assemblies.Names)
cfg.AddAssembly(name);
return cfg;
}
catch (Exception ex)
{
log.Error(ex);
throw;
}
}
And your web config looks has a section like this...
<persistenceMappingAssemblies>
<assembly name="MyLib.Core" />
<assembly name="ClientProject.Core" />
</persistenceMappingAssemblies>
Hope that helps a bit