Playing with NHibernate RC2

I've ignored NHibernate for much of the summer.  Lately I've been talking to potential clients, trying to drum up some business for Q4 and I've been surprised by how many questions I've gotten about NHibernate.  I've also been pleasantly surprised by the number of hits and emails I've gotten from random people in the NHibernate community since being listed on the nhibernate documentation page

I wouldn't say that there's going to be a rush to NHibernate but there's good buzz around the upcoming “production“ release and when it comes out that there's going to be plenty of companies who start giving it a serious look. 

Anyway, I've started pondering NHibernate again.  In one of my previous NHibernate posts, I wondered if it is worth the effort to learn.  The answer is still “it depends“ but with some new qualifications and ideas.  If you have a small project, you probably don't need nhibernate...just stick with typed datasets, stored procedures, and a code generator.  On that size project you probably don't need any kind of fancy object model because you probably don't have much data validation, your data is probably simple, your data access is simple and bulletproof long-term maintainability probably isn't (and shouldn't be) a huge concern.   

For anything larger where you might actually want an entity-based object model (this is where I've started to come around again to nhibernate), how could you realistically do it except with an O/R mapping framework?  Solving the problem of taking an entity-based object model and turning it into relational data is going to be a nightmare.  You'll end up writing something that has a billion lines of code to traverse your objects with tight coupling between your data access tier and your business entity tier -- OR -- you'll write something that's generic.  If you go the generic route, well, then you've got NHibernate. 

If you use the business entity model you have to solve the O/R problem, so why not use something that's already there?  Why re-invent the wheel?  It's open-source so you could presumably fix any bugs that come up.  Plus, it's not like ObjectSpaces is coming any time soon and the future of LINQ seems a little fuzzy.  Plus, neither of those technologies will have been around the block as much as Hibernate and NHibernate. 

Also my mind has been cleared on two of the big things that made me skeptical of NHibernate:

Issue #1: Well, what if you want to use it in an SOA or webservice context and your entities have circular references in them (ex. parent-to-child with child-to-parent relationships)?  You'll get XmlSerializer/SoapSerializer circular reference exceptions. 

Issue #1 resolved: This wasn't ever really a problem with nhibernate...this is a limitation of the entity-based business model and the .net serializers.  In a discussion with Michael Stiefel (who I still owe lunch to) about SOA, he pointed out that trying to directly serialize the entities is a bad idea to begin with.  There needs to be some kind of adapter in the web service/SOA tier that converts the internal object model to something that is more XML friendly.  The internal object model isn't necessarily a good model for the XML transport. 

Issue #2: If you have to do complex queries and joins for searches, then you'll end up re-creating those structures in HQL and that's a big pain and a big learning curve to accomplish something that could be readily done with SQL.  That's stupid.  Why bother?

Issue #2 resolved: This one was resolved with input from Ben Scheirman.  Ben and I have had a multi-month, on-going discussion on nhibernate specifically around nhibernate session management in ASP.NET.  His solution came as kind of a dope slap.  Don't bother trying to solve the problem with nhibernate at all.  Open up a database connection in the regular way, run the query BUT only retrieve the matching primary key values.  Then pass the keys off to nhibernate and let nhibernate work it's retrieval magic.  To paraphrase, render unto SQL the things which are SQL's, and unto NHibernate the things that are NHibernate's. 

Other news... I refactored my NHibernate sample application a bit...mainly just cleaning up the interaction between BaseDataAccess and the HttpModule.  Nothing major.

Also, I'm working on creating a new and improved sample app with the RC2 of NHibernate and ASP.NET 2.0.  More to come.

-Ben

posted @ Sunday, October 02, 2005 10:36 AM

Print

Comments on this entry:

# re: Playing with NHibernate RC2

Left by Taavi at 10/13/2005 5:58 PM
Gravatar
HOw is your sample app with Nhibernate and asp.net 2.0 going ?
i have been trying to mix the too things myself, but im always having trouble with the nhibernate configuration. It cant find the mapping files :(
Tryed to have the DAO layer in seperate project amd also in the web site project. dident work :(

# re: Playing with NHibernate RC2

Left by Mike at 10/14/2005 5:41 PM
Gravatar
Taavi-
I have it working with 2.0 so here are couple of things you might want to check:
- The mapping file should have a .hbm.xml extension and be embedded resources (this part is what I always forget).
- Use the Configuration.AddAssembly() method to load the assembly with the embedded mapping files.

# re: Playing with NHibernate RC2

Left by Greg Banister at 2/21/2006 11:40 AM
Gravatar
Re: Issue #2:
Have you considered the ICriteria Interface and the Sesson.CreateCriteria?
From the nHibernate doc "Criteria is a simplified API for retrieving entities by composing Expression objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set."
eg:
IList cats = session.CreateCriteria(typeof(Cat))
.Add( Expression.Like("name", "Iz%") )
.Add( Expression.Gt( "weight", minWeight ) )
.AddOrder( Order.Asc("age") )
.List();

# re: Playing with NHibernate RC2

Left by Benjamin Day at 2/26/2006 10:27 AM
Gravatar
I've definitely worked with the ICriteria interface. I like it a lot but it is easiest to use when you're doing single class queries. IQuery is better for anything where your query spans multiple objects.

By the way, I've become a big fan of IQuery and I use it a ton in my "rights-based security framework" sample. (see http://www.benday.com/nhibernate)

-Ben

# re: Playing with NHibernate RC2

Left by Jeremy Hadden at 3/20/2006 9:27 AM
Gravatar
re: issue #1

Regarding your comment "trying to directly serialize the entities is a bad idea".

If your entity objects are light-weight data-only objects why can't they be "xml friendly". Do you maybe have any other posts that talk in more detail about this point?

# re: Playing with NHibernate RC2

Left by Benjamin Day at 3/20/2006 9:57 AM
Gravatar
Hey Jeremy,

It's not so much that the entities are not XML friendly as the XML Serializer for .NET-based web services is not friendly to the parent-child-parent (circular reference) construction.

This fits in with Michael Stiefel's (<a href="http://www.reliablesoftware.com">link</a>) point that this parent-child-parent structure is very nice for the domain model object but that it isn't useful or relevant in the context of a SOA message. Therefore, you'll probably WANT to architect in an adapter layer that converts your domain objects into a more SOA-centric representation.

-Ben

# re: Playing with NHibernate RC2

Left by Jason at 8/23/2006 9:08 AM
Gravatar
I'm curious about this:

Open up a database connection in the regular way, run the query BUT only retrieve the matching primary key values. Then pass the keys off to nhibernate and let nhibernate work it's retrieval magic.

Is there sample code on this? Or a more in depth description?

Thanks for all the great info on your site, you're my primary nHibernate source of info.

# re: NHibernate - quando un modello open source toppa

Left by Tecnologie .NET (Dotnet) at 11/22/2006 11:51 PM
Gravatar

# re: Playing with NHibernate RC2

Left by Symon at 2/5/2007 2:13 PM
Gravatar
My experience with the serializer and web services is that if you're prepared to use RPC encoding rather than the default Document Literal encoding for web services you can model circular relationships.


-Symon.

# re: Playing with NHibernate RC2

Left by jDavid.net at 3/13/2007 1:59 PM
Gravatar
in early attempts it seems that MySQL is not a viable option for nhibernate at this time.

i hope to later evaluate nhibernate on mssql

# Spa business plan.

Left by Business plan. at 7/26/2008 10:44 AM
Gravatar
Business plan example. Surgery center business plan. Free business plan sample. Business plan. Write a business plan.

# re: Playing with NHibernate RC2

Left by KM at 10/19/2009 2:46 AM
Gravatar
Open up a database connection in the regular way, run the query BUT only retrieve the matching primary key values. Then pass the keys off to nhibernate and let nhibernate work it's retrieval magic. To paraphrase, render unto SQL the things which are SQL's, and unto NHibernate the things that are NHibernate's.

My whole perspective of using an ORM was to build your entity based model NOT with the database (just a place to persist our data) in mind. Having said that, directly although NHibernate supports direct SQL execution, doesnt that mean we are now introducing dependeny on our data store?

Your comment:



 (will not be displayed)


 
 
 
Please add 4 and 1 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910