2

I have a generic methode for load the entity. I need to check result value for null value.

public TEntity LoadById(long id)
{
     TEntity result = SessionInstance.Load<TEntity>(id);

     if (result != null)                      //This condition is always true
         if (result.Id == 0 )                 //Throws ObjectNotFoundException 
              throw new Exception("Ex Text"); 

     return result;
}

But my condition ( if (result != null) ) is always true and next line result.Propagate() is throw ObjectNotFoundException exception by this message : No row with the given identifier exists[RCISP.Domain.Entities.Person#1000]

Because result entity is a proxy. How can I check a condition for null value in a proxy?

Ehsan
  • 3,431
  • 8
  • 50
  • 70

2 Answers2

3

Use NHibernate's ISession.Get instead of ISession.Load. Load throws an exception if the requested item does not exist, but it might also return a proxy that is later used to load the object from the database - and will only then throw if the item does not exist. That's what is happening to you.
Get on the other side returns null if the item does not exist in database. Exactly what you want.

More on that topic here.

Community
  • 1
  • 1
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • I am familiar with the `Get` in NHibernate. My project is based on `Lazy Loading`. I need to `Load` in NHibernate. What should I do? – Ehsan Jan 23 '12 at 10:17
  • @Ehsan: What is this `Propagate` method doing? – Daniel Hilgarth Jan 23 '12 at 10:29
  • This method is a mandatory methode after of load a entity. Suppose it is required for the check that is not null. – Ehsan Jan 23 '12 at 10:51
  • @Ehsan: But what does it do? If it accesses properties of the entity there is no need to use a proxy, because it will load the object anyway. – Daniel Hilgarth Jan 23 '12 at 10:54
  • @Ehsan: Your edit doesn't clear up things. Why do you override the key with a static value? – Daniel Hilgarth Jan 23 '12 at 11:40
  • My question is updated again. It line code is for sample for view of this exception. My purpose is prevent of a entity by zero value in Id property. The entity by zero value for Id property is an error maker. – Ehsan Jan 23 '12 at 12:12
3

Daniel's initial answer is correct. According to Ayende's blog, Load should only be used when you know that the item exists in the database.

Load will never return null. It will always return an entity or throw an exception. Because that is the contract that we have we it, it is permissible for Load to not hit the database when you call it, it is free to return a proxy instead.

Why is this useful? Well, if you know that the value exist in the database, and you don’t want to pay the extra select to have that, but you want to get that value so we can add that reference to an object, you can use Load to do so

In your example, the ObjectNotFoundException can only occur when the item does not exist in the database. If you can't guarantee that the item is present, you need to use Get not Load.

Rich Tebb
  • 6,806
  • 2
  • 23
  • 25