I have several issues that I'm working on, because I'm a newbie at
this, and wanted to address the one that I "solved" first.

The problem was that I was noticing that all of my collections were
not loading lazily, no matter what I did to mark them as such.  It
then dawned on me that the problem was the way I was returning the
collection.

For example,

        public virtual IList<Login> Logins {
            get { return new List<Login>(_logins).AsReadOnly(); }
            protected set { _logins = value; }
        }

I did it this way because if you just return _logins, you're actually
returning a reference to the private variable which violates
encapsulation and the whole reason for marking it private in the first
place.

The problem is that the parent object, User, is a proxy.  Any time
that that parent object was accessed, for example, to set
User.LastName, a query for each of the collections was fired.  The
solution was to change the property to this:

        public virtual IList<Login> Logins {
            get { return _logins; }
            protected set { _logins = value; }
        }

I have run several tests in NUnit and watched the queries coming back
to know that this is what's happening.  Additionally, I used the
following:

        Assert.IsFalse(NHibernateUtil.IsInitialized(testUser.Logins));

It passes for the second property implementation and not the first.

The question I have is this:  Is there a way to encapsulate the
private variable but still have a proxy for lazy loading, or is this a
trade off wherein we sacrifice encapsulation for performance?

Any help you can offer is appreciated.

Sincerely,
Robert Eberhart

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to