Skip navigation.

Random ThoughtsAll recent postsThe Strategy for Discoverers and Entrepreneurs

Enterprise Library / NHibernate Smackdown

When writing the back-end for Positive Lookahead, I chose the Enterprise Library naïvely thinking it would free me from some of the boring manual writing of data access code. The experience was quite sour because I had to wrestle too much with the damn Data Access Block and ended up writing even more code than I would’ve written with plain vanilla ADO.NET.

I’ve always hated EntLib with passion because of its viral nature. You drag one “block” in and a whole bunch of others follow. Before you know it, almost all of its DLLs are sitting in your bin folder. And don’t even get me started on the Composite UI Application Block—that’s one ugly monster. I usually make it a point to avoid touching EntLib, but I got suckered this time.

I’ve been fiddling with NHibernate for the past two weeks, going through the docs (PDF), poking around Ben Scheirman’s series of posts on Domain Driven Design with NHibernate (watch also this screencast) and so on. NHibernate has a steep learning curve, but once you get the hang of it, it absolutely rocks!

No tool is perfect, and HNibernate has its fair share of flaws. Lack of decent tooling is one. I only found MyGeneration (calling it “decent” is a stretch perhaps I spoke too soon) and CodeSmith templates (I haven’t tried them). In retrospect, I see this lack of tooling as a good thing as it keeps the RAD crowd at bay.

Why not ActiveRecord?

Being a proponent of Domain Driven Design, I don’t like mixing persistence logic into my domain. Therefore I’m no big fan of CSLA, ActiveRecord, or SubSonic.

NHibernate allows me to keep the two separate, even though mapping them via XML files is extra (manual) work. You can’t have your cake and eat it too.

For a pet project with a simple Customer–Order–OrderDetails schema I’d use ActiveRecord, but I don’t see myself using it in complex projects.

Why not LINQ to SQL?

A friend of mine refers to LINQ to SQL as “methadone for the Access crowd” (the friend shall remain nameless because I didn’t ask a permission to quote him). I tend to agree. What’s the first thing you see in demos? Grab tables, drag ’em on the designer and—boom!—you have a “domain model.” Riiiight.

Maybe we’ll make some constructive progress in how web apps are built with .NET once people understand that their database schema is not their domain model. Relational model is simply not fit to adequately model real-life concepts and processes. That’s what an Object-Relational Mapper, like NHibernate, is for. We’ll see if LINQ to Entities gets it right.

Comments

Comment permalink 1 Mike Griffin |
MyGeneration Rocks dude, CodeSmith is only just now in 4.0 implementing what we have alredy done ;)
Comment permalink 2 Milan Negovan |
No hard feelings there, Mike. :) For the life of me, I just couldn't grasp MyGeneration.

I should have been more specific: I know MyGeneration lifts some seriously heavy weight, so my comment is limited to NHibernate only.
Comment permalink 3 Jason |
>>database schema is not their domain model

It is very easy to research and learn about the MECHANICS of what you are saying (i.e. HOW to separate these two things). It is much harder to learn the theory of the WHY.

Can you post or link to an example that illustrates why this is a good idea?
Comment permalink 4 Julian |
I aggree that NHibernate rocks, this was the article that got me really hooked: http://www.codeproject.com/aspnet/NHibernateBestPractices.asp

And then I found the code-gen tool SmartCode:
http://www.codeproject.com/useritems/SmartCode-Code_Generation.asp
With its NHibernate templates:
http://www.codeproject.com/useritems/NHibernate_Templates.asp

A great combo - and open source too.
Comment permalink 5 Milan Negovan |
Jason: IMO, the best resources on this topic are Eric Evans' book Domain Driven Design which has a lot of theory. A more hands-on book is Applying Domain Driven Design and Patterns. Other than that, there's a lot about it on blogs.
Comment permalink 6 Jason |
Thank you!
Comment permalink 7 Nima |
nhibernate code generation, this is a problem
I tried codesmith, my generation, power designer, nconstruct, genwise. the generated code is not good. Is there any other tools

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):