Logo

NHibernate

The object-relational mapper for .NET

NHibernate isn't integrated to the compiler, so what?

I know, this title sounds like: "hmm?" Let me explain it with more detail.

NHibernate is a framework that use natively Xml in order to configure a mapping between objects and tables, is in charged of join these different worlds.

But now, what is going wrong with this? Many people can say that Xml files are evil, because you should write a lot of lines, and when you build your project, the compiler doesn't know if its ok or not, you have none errors. And this is true, you should run your project and see what happen in runtime, and to get an error in runtime is just annoying.

But now, do we have another alternative to this? Yes of course. Lets go to details. NHibernate has a "compiler" too, and you can guess what it is: BuildSessionFactory() method. You don't have to launch the whole application to know if your mapping is working well. A way to know if everything is ok is create a test like this:

Then you just need to build the project, and run this simple and dummy test, if your code pass though this, your mappings are ok.

We should remember one thing, you can map into Xml not just entities, you can map queries too (and another stuff that isn't part of this matter). To map queries into Xml it calls: Named Queries, and in this post we talk about this matter. But the main point is, mapping the queries you will know if them are well formed when BuildSessionFactory() is raised, besides another beauties discussed in that post.

Another thing you should remember is you MUST use the Xsd to validate the mapping file. This is mandatory if you're using NHibernate and you want to spend time programming instead of dealing with mapping errors (and here they come even with not-well-formed xmls). But this is topic to an How to or another post.


Posted Thu, 04 September 2008 03:23:00 PM by darioquintana
Filed under: validation, Xml, SessionFactory, BuildSessionFactory, Xsd, Named Query

comments powered by Disqus
© NHibernate Community 2024