Have you ever had a need to decide whether an object reference to an NHibernate-mapped object is the real instantiated object or an NHibernate-generated proxy used for lazy loading? Here’s how to do it:
if (entityObject is NHibernate.Proxy.INHibernateProxy) { ... }
You don’t often need to make a distinction between proxies and the real objects but it may be useful when traversing object structures without triggering lazy-loading.
A related method is NHibernateUtil.IsInitialized which may come in handy in similar use cases as the above. It tells whether a proxy or persistent collection is initialized (i.e. loaded from the database) or not.
if (NHibernateUtil.IsInitialized(entityObject)) { ... }
/Emil
Ooh, this SO calls for extension methods!
Wouldn’t entityObject.IsProxy() and entityObject.IsInitialized() look much nicer? 🙂
Perhaps, but that wouldn’t be very educating in a blog post about NHibernate, would it? 🙂