nHibernate proxies for lazy loading - Ed's Blog
in

dotnet.org.za

South African .NET Developer Portal

Ed's Blog

Object reference not set to an instance of an object

nHibernate proxies for lazy loading

nHibernate has always used lazy loading for collections (one-to-many and many-to-many relationships) but you can now use lazy loading for one-to-one and many-to-one relationships (from v0.6.0.0).

For example, let's say you map a Department/Proffesor relationship where the Department class will have a collection of Proffesor objects and the Proffesor class will have a Department property which returns a Department instance. In previous versions of nHibernate, when you load a Proffesor object, it's Department object will be loaded automatically, whether you're gonna use it or not. But now with proxies, it will not load the department until the first time you access it.

nHibernate actually generates IL dynamically to achieve this, which I think is pretty cool.

The only catch is that you must either use an interface for your Department class or all the methods and properties on the class must be declared “virtual”, which shouldn't be a big problem.

Bottom line - How cool is nHibernate!!!!!! If you're coding an OO system with a relational DB, do yourself a huge favour and check it out.

Published Jan 25 2005, 12:14 PM by eduard
Filed under:

Comments

 

Armand said:

I'll second that!
January 25, 2005 1:41 PM
 

Tadej Mali said:

Hi.

I am trying to create a many-to-one mapping, where I will only need a key of the "many" object in the "one" object. E.g. I have a class "Contract", which belogs to one "Customer", who of course can have several contracts.

Suppose that I get the contract Id somehow (e.g. customer calls and tells me the contract number) an I fetch the contract. The catch is, that in the case when I need to display contract and the customer id, NHibernate will pull up the whole Customer object, even though I need only id, which is actually already stored in the row as the key of many-to-one relation.

Any ideas how to workaround this?
March 2, 2005 4:28 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems