Kamil Kliczbor @ asptip.net


NHibernate processing *.hbm.xml order


Recently we had small problem with defining views in our mappings. Because we use mapping export to local MS-SQL database we needed to define the views in our *.xbm.xml files. The problem was arising when we wanted to have dependencies between views. First thing we thought it would make it work was to put the views in the order defined by hbm's names. That did nothing. Then we started to dig in the FluentNH code and we went deeper in NH then found these interesting lines of code in the Configuration.cs:

/// <summary>
/// Adds all of the assembly"s embedded resources whose names end with <c>.hbm.xml</c>.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>This configuration object.</returns>
public Configuration AddAssembly(Assembly assembly)
	IList resourceNames = GetAllHbmXmlResourceNames(assembly);
	if (resourceNames.Count == 0)
		log.Warn("No mapped documents found in assembly: " + assembly.FullName);
	foreach (var name in resourceNames)
		AddResource(name, assembly);
	return this;

private static IList GetAllHbmXmlResourceNames(Assembly assembly)
	var result = new List<string>();

	foreach (var resource in assembly.GetManifestResourceNames())
		if (resource.EndsWith(".hbm.xml"))

	return result;

The answer was clear: the mappings are added to the queue (see class MappingsQueue.cs) and processed one by one in the order they were enqueued. And the order depends of the internal implementation of the GetManifestResourceNames method. Unfortunately this boils down to know the implementation of the method from System.Reflection.RuntimeAssembly that is quite hard to decompile and investigate.

private static extern string[] GetManifestResourceNames(RuntimeAssembly assembly);

However from experiments we found out that the order of returned resources depends on the order they are defined in the *.csproj file.

Special thanks to Grzegorz S. for taking part in our experiments.

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.