Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Verifying that that your data is Eagerly loaded in NHibernate

In my last post I talked about how I needed to eager load my data in order to send it back across the wire.  Normally I am not a big fan of creating tests to ensure that I can hit the database, but in this case the act of eager loading needs to be verified and constant. 

When I set out to create my tests, I was not exactly sure how I could verify this.  The good news is there is a utility class called NHibernateUtil which has a method on it called .IsInitialized() which will return a bool.  Below is the unit test I put together to ensure my data was always being eagerly loaded.  Yes I know that this test does NOT take into account any new data which may need to be eagerly loaded, but it is at least a step in the right direction.

public void FetchDestinations_EnsureDataIsEagerLoaded()
    var repository = new DestinationRepository();

    IList destinations = repository.FetchDestinations();

    // want to ensure that the data was eager loaded as we need/want
    var destination = destinations.FirstOrDefault();

    // incase there is no data in the DB
    if ( destination != null )
        Assert.That( NHibernateUtil.IsInitialized( destination.Endpoint ), Is.True );
        Assert.That( NHibernateUtil.IsInitialized( destination.AssignedMessageTypeses ), Is.True );                

As you can see from my test, I do need to add in come logic to ensure I am hitting my database and gathering data.  I am normally not a fan of this, but I think it is needed here as I do not want to fail my test if there is no data. When this test runs I will always know that my data is being eagerly loaded which fulfills my needs.

Till next time,

Posted 03-06-2009 7:27 AM by Derik Whittaker
Filed under: ,


Comments wrote Verifying that that your data is Eagerly loaded in NHibernate
on 03-06-2009 10:10 AM

You've been kicked (a good thing) - Trackback from

NHForge wrote re: Verifying that that your data is Eagerly loaded in NHibernate
on 03-06-2009 10:37 AM

You can find a similar post here:

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)