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 article updated

I've updated the CodeProject article NHibernate Best Practices with ASP.NET, 1.2 ed with a number of very important fixes and pedagogical expansions.  (I've been waiting for an excuse to use that word.)  If you've been using the sample code as a foundation for your project, these changes will be important to you.  Here are a few:

  • Example code has been added to demonstrate using NHibernate with custom collections.  A new page called ListSuppliers.aspx then leverages the custom collection.  The article has also been updated to explain the technique.
  • The Model-View-Presenter example has been expanded to include the EditCustomer.aspx page; this page contains three views on a single page with event handling and application flow separated from the views.
  • Overhauled the DomainObject base class to be in-line with a previous post concerning the use of Equals and GetHashCode.
  • NHibernate can return strongly typed lists, natively.  The generic DAO, included with the sample, now includes this capability.
  • Bug:  Transaction rollbacks were not previously working.  This has been fixed.
  • Bug:  Closing the NHibernate session was not flushing changes made to data.  So if you weren't using a transaction, changes weren't being committed; this has been fixed.
  • Date manipulation has been fixed to allow the unit tests to pass in different localizations.
  • Added support for IIS 7 to work with the HTTP modules.
  • A number of other fixes and enhancements have been included.

I'll be kicking off a contest later this week concerning a needed refactoring to the "enterprise" sample code...only the first correct submission will be a winner, so stay tuned to devlicio.us for details.

Billy McCafferty


Posted 05-02-2007 4:24 PM by Billy McCafferty
Filed under: ,

[Advertisement]

Comments

links for 2007-05-03 » mhinze.com wrote links for 2007-05-03 » mhinze.com
on 05-03-2007 11:23 AM
Connor Peterson wrote re: NHibernate Best Practices article updated
on 05-15-2007 8:53 AM

Billy,

Just a quick question -- why is the BeginTransaction code in the NHibernateSessionModule commented out?  Does the transaction get initialized somewhere else that I'm not seeing?

Thanks

Connor

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 05-16-2007 10:56 AM

This was a mistake...it should not be commented.  (That's still sitting on my to-do list.)

kellyb wrote re: NHibernate Best Practices article updated
on 05-19-2007 9:13 PM

Billy,

While I've looked at NHibernate several times in the past, I've selected to use other frameworks for one reason or another.  I very much like the POCO based design, which keep bringing me back to NH.  I enjoyed reading your article and I'm looking through the code.  Something that stands out:  While watching ListCustomers.aspx run with SQL Profiler running, NHibernate seems to be make many, many queries.  It appears a SELECT is issued for each individual Customer row (nearly 100), when they could all be easily fetched with a single query.  Is this normal NHibernate behavior?

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 05-20-2007 9:01 PM

Good catch kellyb...that's a bug which I'll be addressing shortly.  It wasn't NHibernate's fault...just mine.

Giacomo wrote re: NHibernate Best Practices article updated
on 05-22-2007 4:17 AM

I discovered the same behavior except for objects that don't have relations (bags and many-to-one).

Thanks Billy.

Giacomo wrote re: NHibernate Best Practices article updated
on 05-23-2007 4:59 AM

The problem resides in the property getter of the collection (get { return new List<Order>(orders).AsReadOnly(); }).

PartialClass wrote re: NHibernate Best Practices article updated
on 05-31-2007 1:03 AM

Billy, I have downloaded the basic sample and there is a question for u.

Uptill nowm wat i have done in my projects is that i add reference to dataaccesslayer , from BusinessLayer.

But here , u have added the ref. of businessObj to dataaccesslayer. The Data project contains only the implementation of the interfaces + some session management code for NH.

Wat i think is that DAO interfaces and their implementation shud be kept together in seperate assemblies.That assembly shud be referenced by DomainObj.

Does that make any sense?

Thomas wrote re: NHibernate Best Practices article updated
on 06-27-2007 6:17 PM

Hi Billy - I have a question regarding the use of MVP/NHibernate in combination with some of the standard databound control provided by the ASP.NET 2.0 framework.

If I want to create a small user control that takes care of editing an entity I would normally opt for a FormView control. However, using a FormView control within a "View" in the MVP sense I would have to become very familiar with the events of the FormView in order to delegate work to the Presenter, which seems to be a lot of work.

I seems to me that to go true MVP I would have to abandon FormView and simply create a set of Panels with controls that I enable as needed depending on whether I am viewing, editing or inserting.

What is your opinion on that?

When you work with MVP, do you use the built-in databound controls or do you create your own "simpler" views?

Thanks in advance.  :-)

Thomas

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 07-01-2007 4:21 PM

I generally subscibe to the "Supervising Controller" form of MVP so as to leverage advanced user controls, such as Telerik, when possible.  This sometimes grays the line between what should be done in the view and what should be done by the presenter, but if the work's already being done by the user control, I generally don't try to reinvent the wheel.

Eric G wrote re: NHibernate Best Practices article updated
on 08-02-2007 11:05 AM

Thanks Billy for the great article.  I've been looking at NH for some time and your article convinced me that this is a great ORM.

I have one inquiry though on how NH can be used in a more generic way.  I would ideally like to build a DAL component that can be used by multiple projects.  

Your article talks about IoC pattern by which the DAL references the Biz layer, using interfaces to the biz objects.  This is perfect for a single project, but I don't see how this would work for a DAL component that is used by N projects.

what are your thoughts about this?

Lisa wrote re: NHibernate Best Practices article updated
on 09-10-2007 3:08 AM

Hi Billy,

I am still in confuse about transaction management.

As you know, when working with NHibernate, we begin with sess.BeginTrans and finish with sess.Commit or sess.Rollback.

In your article, you use a HttpModule to add process on BeginRequest and EndRequest to do the transaction management. But what will happpen when the transaction fail? I cannot see the code when you call Rollback transaction.

Please will me more explanation about this.

RhysC wrote re: NHibernate Best Practices article updated
on 01-07-2008 10:16 AM

Hi Bill

On your article about NHibernate best practices you mentioned a idea to create a guidance package to perhaps help with the creation of the DAL and objects.

To the best of your knowledge has anyone tackled this challenge?

I am thinking of giving it a go, not knowing much about creating GP it should be an interesting exercise.

Regards

Rhys

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 01-07-2008 11:48 AM

Not as of yet; coincidentally, it looks like I'll have a need to do this in the next month or two.  I'll certainly keep the readers here posted if I do so...and please let me know if you beat me to the chase.

Sean Gilbert wrote ADO.NET Entity Framework with Oracle
on 11-20-2008 2:50 PM

ADO.NET Entity Framework with Oracle

Ubestim wrote re: NHibernate Best Practices article updated
on 11-25-2008 12:50 PM

Hello,

 I'm an NH beginner , your article is so great ! I learned more besides NH.  

I find there is no code to demonstrate how to delete an instance ,can you add some ?

I know when I call Isession.delete(obj), the instance become detatched, will it be removed from the underlying datasource?

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 11-25-2008 12:53 PM

Yes, calling Delete will detach it and then delete it from the DB when the ISession gets flushed.

Ubestim wrote re: NHibernate Best Practices article updated
on 11-25-2008 1:13 PM

sorry I mean the datasource control like objectdataSource

Ubestim wrote re: NHibernate Best Practices article updated
on 11-25-2008 1:17 PM

wow!  Time in my place is 2:16 am , I must go to sleep now!!

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 11-25-2008 2:17 PM

If you delete the object, I would be phenomenally surprised if it also got removed from ObjectDataSource during the same HTTP request.

TD wrote re: NHibernate Best Practices article updated
on 11-26-2008 2:04 PM

I'm seeing a problem along the same lines as what Giacomo described back in May. I have a many-to-many bag, and NHibernate deletes its contents when I Flush the session. It definitely has to do with the collection getter. If I change it from return new List<Order>(orders).AsReadOnly() to simply return orders, it works fine.

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 11-26-2008 4:05 PM

Thank you for brining this up TD.  In my current work, I've been encapsulating collections as follows which avoids this altogether:

public IList<Order> Orders { get; protected set; }

...and then initialize the collection in the constructor to new List<Order>()

grifoxx wrote re: NHibernate Best Practices article updated
on 02-07-2009 4:52 AM

Hi Billy,

I've been reading your EnterpriceSample and I find very useful.

I have a question converning the Castle framework. I notice that you set a path in the CastleComponents.config file for the NorthwindNHibernate.config. So I was wondering what happened if I want to deploy my project on Hosting Provider. I mean I won't have any idea where the config file will be placed so I would have to use a relative path.

How would you do that? I will appreciate any suggestion

Thanks a lot

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 02-20-2009 3:10 PM

Hi grifoxx,

If I may suggest, you may want to check out S#arp Architecture which was born out of this article.  This newer architecture addresses many of the problems that existed in the article.

Bat wrote re: NHibernate Best Practices article updated
on 03-24-2009 12:20 PM

Hi Billy, first thanks for those great article. I have a confusion regarding collections. Having the following:

public IList<OrderLine> OrderLines {

           get { return new List<OrderLine>(_orderLine).AsReadOnly(); }

       }

I expected that the collection OrderLines was ReadOnly but still i can modify OrderLine properties from grids. How can i make sure that  OrderLine is true ReadOnly?

Thanks

Billy McCafferty wrote re: NHibernate Best Practices article updated
on 03-24-2009 12:26 PM

If you want read-only objects, you can make them immutable (which probably isn't what you want), or you convert the object data into data transfer objects to be given to the view which only have get accessors.

Bat wrote re: NHibernate Best Practices article updated
on 03-24-2009 1:13 PM

I get it, that confuse me, beacuse as one read "AsReadOnly" my firts instinct was "i will get a totally readonly collection for free".

Thanks Billy, keep good work

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)