Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Billy McCafferty



NHibernate Best Practices with ASP.NET, 1.2nd Ed.

I'm very pleased to announce the release of NHibernate Best Practices with ASP.NET, 1.2nd Ed. on CodeProject.com.

Preface to the 1.2nd Edition 

In March of 2006 I published my initial thoughts on NHibernate best practices with ASP.NET, generics and unit tests. I've been delighted to learn that these ideas have been implemented in a number of real-world scenarios with strong success. Since then, I've worked with many people to refine these ideas, learn from mistakes and leverage a more powerful version of NHibernate. Accordingly, although only a modest, yet important, amount of modification has been made to the underlying architecture, some other important factors have been updated and addressed in this article:

  • Quite simply, NHibernate is awesome. In the previous edition of this article, I assumed you already knew this...but I now try to convince the dissenters as well.
  • NHibernate 1.2 natively supports generics.
  • NHibernate 1.2 natively supports nullable types.
  • NHibernate 1.2 natively supports mapping attributes.
  • NHibernate 1.2 can communicate with stored procedures.
  • Using CallContext for ISession storage in ASP.NET was susceptible to failure under load.
  • Exposing a publicly settable ID property created a point-of-susceptibility.
  • Providing automatic parent/child wiring, via Ayende's very helpful NHibernate.Generics, was more headache than help.
  • Have you used Rhino Mocks 3.0, NUnitAsp, or Fit? Well, these are all discussed with an expanded emphasis on test-driven development.
  • As an alternative to my recommendations, also consider Castle Project's offerings such as MonoRail and/or ActiveRecord for a simple yet powerful out-of-the-box framework for ASP.NET. In fact, if it's technically feasible and you can generate buy-in on your team for these off-the-shelf tools, I would recommend using them over a ground-up solution. But even if you do use Castle Project facilities, this article should still have a lot of useful information for you!
  • In addition to those listed above, there are other important refactorings and fixes throughout the article and the code. This edition is by no means just a light touch-up of the original article.
  • In addition to an overhaul of the original sample code, an expanded "enterprise" sample has been included demonstrating:
    • NHibernate with web services
    • NHibernate with multiple databases
    • Integration with Castle Windsor
    • A reusable data layer for the data access components.
    • A simple example of Model-View-Presenter

I hope this article will be chock full of (at least a couple) good ideas no matter what your level of experience is.  I look forward to hearing your experiences with the article and any feedback you may have.  I've also included some areas for further research for anyone interested in taking the sample framework further.

Billy McCafferty



Comments

Frank said:

I implemented a project based on your first article, and it all went really really well! Now the second edition is a really great addition to an allready great article! Thanks for all your time and efforts! I have not read everything from the second edtion atm but the topics and parts i read are really great.

# April 4, 2007 5:57 AM

Billy McCafferty said:

kamal,

I run some experiements and report my findinings along with any necessary solution.

Frank,

Thanks for the feedback!

# April 4, 2007 8:41 AM

Liming Xu said:

Billy,

Your blog will be my must-read blog now. Thanks for your hard work! You are definitly THE person to watch for when it comes to userful examples on NHibernate, it's just so hard to find any good NHibernate tutorials out here, thanks again for fill in the gap.

Unrelated note, I really like the skin by soup.co.za, how can I get to install this as well? Is this a community server 2.1 version?

Thanks

# April 4, 2007 9:40 AM

Jon said:

Billy

Great update to the original - I completely agree with the section on application layer structuring.  Ironically, if one employs Spring.NET in their project, you *have* to use the layers in accordance with the diagram you laid out...  If anyone reading hasn't already gotten familiar with Dependency Injection and IoC, do it  - it will make life much easier.

# April 4, 2007 6:57 PM

ryzam said:

Hi Billy after read kamal comment, i try to investigate the httpmodule and find every image in the page will have their own http request to the server, however we can filter the request not to invoke NHibernate Transaction with adding a few line of codes.

private void BeginTransaction(object sender, EventArgs e) {

           HttpApplication app = sender as HttpApplication;

           string strUrlRequest = app.Request.Url.ToString();

           if(!strUrlRequest.Contains("JPG") || !strUrlRequest.Contains("GIF") )

           {

               LogManager.GetLogger("Basic").Info("Begin Transaction ");

               NHibernateSessionManager.Instance.BeginTransaction();

               i++;

           }

           else

           {

               LogManager.GetLogger("Basic").Info("Image");

           }

       }

# April 12, 2007 12:05 AM

Damon Carr said:

Again... NICE JOB!!!!!!

Damon

# May 7, 2007 7:49 AM

Shiva said:

Hi Billy,

Thanks for this well researched and elegantly written article.

Does this article encompass the March 2006 article you are referring to ? If not, where is it ? I tried to find it on codeproject and in this blog and came up empty.

# August 5, 2007 8:51 PM

Josh Berke said:

Think I ran into a bug with the latest version of nHibernate and your recommendation. Thought I'd let you know: jberke.blogspot.com/.../nhibernate-transaction-session-mgmt.html

If I'm wrong let me know and I will remove the post.

Enjoy

-Josh

# October 27, 2008 6:35 PM

Billy McCafferty said:

Great catch Josh!  I've responded on your blog post.  Thank you for brining up this item.

# October 27, 2008 7:01 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Proudly Partnered With