Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 04-03-2007 3:36 PM by Billy McCafferty
Filed under: ,

[Advertisement]

Comments

kamal wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-03-2007 10:34 PM

Hi Billy ,

I'm using your NHibernate sample and i try to modify the httpmodule , which adding logging services (log4net). In Add Customer page i just add simple img logo, then i execute the page, i see in the log file it will invoke Begin_Request twice, if i insert second img it will adding extra Begin_Request. Is it bugs??

Frank wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-04-2007 5:57 AM

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.

Billy McCafferty wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-04-2007 8:41 AM

kamal,

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

Frank,

Thanks for the feedback!

Liming Xu wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-04-2007 9:40 AM

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

Jon wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-04-2007 6:57 PM

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.

ryzam wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 04-12-2007 12:05 AM

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");

           }

       }

Damon Carr wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 05-07-2007 7:49 AM

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

Damon

Shiva wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 08-05-2007 8:51 PM

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.

Josh Berke wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 10-27-2008 6:35 PM

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

Billy McCafferty wrote re: NHibernate Best Practices with ASP.NET, 1.2nd Ed.
on 10-27-2008 7:01 PM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)