sharing about .NET and technology RSS 2.0
# Monday, October 20, 2008

NHibernate uses an xml file to describe the mapping. Typically it is embedded as a resource file inside your project and it has a consistent filename convention, namely <classname>.hbm.xml. Typically an NHibernate project has the following structure

Structure 1

Would it not be nice if we can organize it in the following way? I found it always handy that related classes, like designer classes and resource files are grouped together.

Structure 2

To achieve this structure we can start from the first structure and nest the xml file trough a macro that I’ve written and can be downloaded here (there is also a video how it can be used).

The second step, is to make clear to NHibernate where the mapping files reside. Note that resources that are nested to a class are compiled differently and thus NHibernate will not recognize the mapping files anymore. Therefore we need to traverse manually the assembly and try each embedded resource file. Below you find the resulting code

var domainAssembly = typeof(MyDomain.Order).Assembly;
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();

foreach (var resourceName in domainAssembly.GetManifestResourceNames())
{
   try
   {
      cfg.AddResource(resourceName, domainAssembly);
   }
   catch (NHibernate.MappingException)
   {
      //ignore
   }
}
Monday, October 20, 2008 12:16:35 AM (Romance Daylight Time, UTC+02:00) -  # -  Comments [2] -
.NET | NHibernate
Navigation
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Christoph De Baene
Sign In
Statistics
Total Posts: 176
This Year: 2
This Month: 0
This Week: 0
Comments: 283
All Content © 2010, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene