Lazy load and proxy object

14 views
Skip to first unread message

RoyWagner

unread,
Mar 19, 2009, 12:36:06 PM3/19/09
to nhusers
This is the situation:

I have an abstract Profile class and 2 derived classes let’s call the
ProfileA and ProfileB.
Then mapping structure that I use is table per class hierarchy.

I also have a user class that have reference to a Profile:

Public class User
{
public virtual Profile Profile { get; set; }
}

I use lazy load for the Profile property.

The problem is that when try to cast the User.Profile to one of the
derived classes(PofileA or ProfileB) I get an error that I can’t cast
ProfileProxy16982… to ProfileA.
NHibernate create ProfileProxy object in order to support the lazy
load and that prevent me from casting the Profile object.

The only solution I have right now is to cancel the lazy load.

rashack

unread,
Mar 20, 2009, 10:43:38 AM3/20/09
to nhusers
Hi Roy,
there's a way how to get around this although it involves having
reference to your session...:

ProfileB b = session.Get<ProfileB>(user.Profile.Id);
instead of doing your downcasting (which is not really OOP pure). It
should not ivolve a database hit since the profile should already be
loaded in the session first level cache.

Hope this helps,
Jarda
Reply all
Reply to author
Forward
0 new messages