26 March 2007
NHibernate Tutorial, Jumptree Forum Part 5- Different Ways of Mapping File Configuration
Part I - Why I choose Nhiberneate
Part II - Spring.Net, Setup Nhibernate Support
Part 3 - Getting Ready
Part 4 - Setup and Add Category
In yesterday's post, I mentioned there are several different ways to configure your mapping files for usage in ASP.NET 2.0, so tonight, let's talk about it.
First of all, if you recall from my previous discussions, the way ASP.Net sets up NHibernate is through the Application Object. If you followed my tutorials, then you know I used the default "ExampleApplication.cs" from the default Nhibernate Web Example supplied via the NHibernate download.
Secondly, since I've only done "Adding" categories to our application, so I'll be using the "JumptreeForum_Categories.cs" class and its mapping file as an example.
But first, let me show you what I did WRONG at first before we continue onto the right ways.·
Wrong Way
Mapping ·File
HttpApplication File
In the first screen shot, as you can see, I defined my class in the mapping file, nothing special. In the second screen shot, as you can see, I added the assembly "Jumptree.Forum.BusinessEntities" to Nhibernate because I figured that's all you need for Nhibernate to automatically scan through the assembly and find the embedded .hbm.xml files.
Well, I got the error "Could not load type ... no assembly found" when I loaded my web page.
A quick search through the official Nhibernate discussion board, I found this link
Jason described:
"This error indicates that the class could not be found when NHibernate is initializing. The key here is "from assembly NHibernate" The reason I was getting this error is because I didn't realize that you have to specify not only the name of the class (including the namespace prefix), but also the assembly name after the class. I would have thought that NHibernate would have known the assembly name since I was giving it the full namespace, but apparently this is not the case."
He continued with the following example and said the follwing:
<class name="myassembly.model.Foo" table="foo">
"This is not enough information. You must also specify the assembly name, or else NHibernate uses the default assembly name of "NHibernate" It should really look like this:"
<class name="myassembly.model.Foo,myassembly" table="foo">
"this also applies when mapping relationships: "
<many-to-one name="child" column="child_id" class="myassembly.model.Child" not-null="true"/>
"should be: "
<many-to-one name="child" column="child_id" class="myassembly.model.Child, myassembly" not-null="true"/>
Okay, fair enough. So I added the full namespace and assembly into my configuration file like the following which will be our
Way 1
Mapping File
As you can see, my class name contains the full namespace + class name and assembly.
HttpApplication File
Loaded my application, voila, it worked.
·
Way 2
Well, while "Way 1" worked, Jason also pointed out you have to do that for every class name you mentioned inside of your mapping file. It can be the class tag, it can be the one-to-many tag, many-to-many tag and what not. One mapping file is fine, but what if when you have tons of mapping file? That sure is a lot typing isn't it? Is there another easier way? Well yes and here it is.
Mapping File
·
As you can see, my class is "clean" again. I simply specified the class name without namespace and assembly information
HttpApplication File
You see what I did here? Instead of putting the namespace and assembly information in the mapping file, I programmatically used ".SetDefaultAssembly()" and ".SetDefaultNameSpace()" method. Write in one place, that's it. ·
Loaded my application, again, it worked. Cool.
Way 3
Okay, Way 2 was nice, but man, I wish there is a way that I get the goods of both sides. While having the flexibility of configuring the namespace / assembly inside .hbm.xml file, but at the same time, just do it once and not have to type it over and over again in any places a class name appears. Well, again, we do have a way.
Mapping File
As you can see, I added the attribute “assembly" and "namespace" to the root <hibernate-mapping> tag. Sorted as if I declared it on a
"global" level.
HttpApplication File
As you can see, compare to "Way 2", I got rid of the ".SetDefaultAssembly()" and ".SetDefaultNameSpace()" method.
Loaded the application once more, worked.
So there it is, three ways of configuration your mapping files within ASP.NET 2.0. Now, before I end this session, I need to mention, I don't like the way I hard coded the assembly "Jumptree.Forum.BusinessEntities" into the HttpApplication file.
Instead, I ·added an "appsetting" key "HBM_ASSEMBLY" inside of the web.config and configured my assembly name "Jumptree.Forum.BusinessEntities" there.
Here is how my end result looks like
Okay, hopefully, I explained enough and I surely hopes it helps you understanding how Nhibernate works in regards to this issue.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.