The main application I develop for my employer uses NHibernate, which
was mine and 1 other developer's decision. When we started our
application (an ordering system of sorts), neither of us had ever used
NHibernate, or any other ORM framework. Setting up the mapping files
was difficult at first. Initially, we set up everything to use lazy
loading, because it seemed like it would be the most efficient. Don't
get the data until you need it. Awesome!
Ok, so we have our base User table, and we have a Mapper table (GIS
professionals). Some users are mappers, but not all. We didn't do
any inheritance mapping or anything, we just used good old
composition. So, if I have a mapper object, and I want to get their
name, it might go like this:
Console.WriteLine(mapper.User.FullName);
That's great, but what about this?
IList<Mapper> mappers = session.CreateCriteria(typeof
(Mapper)).List<Mapper>();
foreach(Mapper mapper in mappers)
{
Console.WriteLine(mapper.User.FullName);
}
Each iteration through the loop will cause the current mapper's User
object to be lazy loaded, resulting in a new and separate call to the
database. In our web application, we often have to populate a drop
down list with the names of all the mappers, which results in code
just like what I wrote above.
I put some traces on our database, and took a look at the number of
queries when loading a simple page in our application. Over 150
individual queries! Just to load a page! So now the Mapper mapping
file looks like this:
<many-to-one name="User" column="userID" unique="true"
access="field.camelcase-underscore" fetch="join" lazy="false" />
Then I took a look at a few of our mapping files, and realized that
certain objects will ALWAYS be needed together, never apart, so why
use lazy loading in those situations? I made sure none of my "eager"
fetching settings would end up spiraling huge objects graphs out of
control (with cycles in the graph), and ended up changing only 3 or 4
relationships to use eager join fetching.
AND GUESS WHAT! Those 150+ queries for a single page load dropped
down to 33 queries! The WHOLE APPLICATION is noticeably snappier now.
Lesson learned. Don't blindly program to a paradigm or use a specific
feature, because you have some generalization in your head that it's
"the best way". Analyze your specific needs, understand your tools,
and make the best choice on a per-case basis.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" 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/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---