11 March 2007

NHibernate Tutorial, Jumptree orum Part I - Getting ready to use Nhiberate. Why I choose NHibernate and what will I implement?

Part II - Setup Spring.NET Nhibernate Support
Part 3 - Nhibernate Forum - Part 3, Getting Ready
Part 4 - Setup and Add Category
Part 5 - Different Ways of Mapping File Configuration

I've been trying to avoid NHibernate for a long time and I just realized that I can't forever ignore it if I want to really save myself time in the long run.

I've been doing my own data access layer for a long time and in my recent projects, the reason that I persuaded myself away from NHibernate is because

  • For the time being, I only need to support SQL Server, no other databse is needed.
  • It’s hard to pick up. Have you been reading the stuff around? Hard to even collect all the material together for me to get started.
  • I have been using the Web Service Software Factory (asmx version) It really saved me a lot time because it auto generates my project skeleton, have GUI tools that maps each database table to an entity class and then generates the corresponding data access layer all within the convenience of Visual Studio, but there are a few inconveniences to it.

    Whenever my database changes, I had to delete my original entity and data access classes and re-generate. Ultimately, I had to resolve to an “-ext” extension classes that inherit from the auto-generated entity classes to avoid conflicts.

    The mapping tool has issues recognizing inherited classes (not that big of a deal as inherited classes are in fact a type of their parent, so auto-generated data access classes still accepts our “-ext” classes as parameter. Does that make sense?

    No Transaction is supported within the auto-generated code. Much of my data access layer involves transactions (saving, updating multiple tables at a time), such I had to abandon much of the auto-generated data access classes. (*Note, I can’t use the new TransactionScope class because 1. It doesn’t run in a medium trust environment (as least not with my hosting company). 2. We have SQL server 2000, this class is only efficient when it’s used with SQL server 2005)

    The only reason I kept using it is because they generated one table per entity class  and they auto-generated the “set-entity-properties” classes that I needed everywhere. As you know, manually retrieve data from a table and then setting each property to the column values is very tedious (I have LOTS of tables) . In addition, the .ASMX service layer helps me setup the web services structure. All these together, is a lot better than if I had coded them manually.
  • SubSonic by the Rob Conery of the Commerce Starter Kit. I personally have not yet tried it but I have paid a lot close attention to his stuff lately. If you watched the demo Rob did with Subsonic, I guarantee you’ll wet your pants. (The only reason that I’m still waiting is the transaction support)

    Sometimes I don’t know what Rob has been eating.  His brain seems abnormal. How the hell one person is able to create such an excellent framework all on his own? I can’t see myself creating anything like that…ever.
  • Linq/DBLinq is on the horizon. I sorted wanted to wait for that as it has a lot of goodies coming with it, but I’m reconsidering it because like usual, it will only come with Sql Server support. No mysql, no postgresql or any other db at least for the time being.

So having considered all these options, I decided now it’s time to give NHibernate a try.  The reason is

  • Build in Transaction support (Missing in Web service Factory and Subsonic)
  • Support multiple databases. Simply change the “dialect” and you are good to go (The list of supported databases is well beyond most of all the other frameworks will EVER support) and because I think our customers will grow into a multi-database environment, there is no other open source framework more robust than NHibernate.
  • No more store procedures. I like store procedures, but the issue is the pain of migrating your database. I have local, test and production database. Every time when I create or modify store procedures (and believe me, I do that a lot), I have a headache migrating them from server to server. I’m not rich, so I can’t purchase the excellent SQL Compare from RedGate, so my solution is to record down what store procedure is changed or added or deleted on my notepad, by the end of the day, I manually compare my local with my test and production database. Then export the added/modified store procedures and delete the not needed ones away from other database. After doing that for project after project,  I screamed “Store procedure no more!” I’m a programmer, I want to focus on coding.  
  • Can we say build in data access through HQL?
  • Support auto database generation from mapping files. Now that’s the convenient! I had to create a web installer for my product to install the database. Believe me, the trouble of creating such a tool is not easy! If I changed some sql scripts right now, I have to re-export the whole thing, get rid of the old ones and drop them into the installer again, where in NHibernate, simply drop in the modified mapping files, you are good to go.

Okay, so now I gave you the history of how I got here, let me tell you what I plan to do. Here are the screen shots of the forum our excellent designer Jackson is planning to build for our website.





It’s nothing complicated. We just want to divide our forum into multiple sections or categories. In each category, our visitors can post their questions without the hassle of logging in. (Of course, to prevent spam, we-admin users will just have to be constantly monitoring it through emails notification)

The requirements are the following

  • Forum that allows both anonymous users (post and edit their own post) and admin users. (admin users will be able to modify any posts and delete any posts)
  • Support multiple categories
  • Support multiple posts per categories.
  • Record User IP (admin view only)
  • Notify admin users of new/updated posts.

Sounds like an interesting project. I’ll use NHibernate to jump start this project and in the next few weeks (sorry, I’m pretty sure it wont’ be smooth, so I’m taking my time with it) I’ll blog about my process including the articles I read, the code I grabbed from others and what I did to make it work using NHibernate, so stay tuned!! (Also a reminder for myself really, if I don't blog about it, I don't have any pressure). We'll see how it goes.

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 RSS

Comments

No Comments

Leave a Comment

Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.

(required) 
(optional)
(required)