Linq to NHibernate in 10 minutes

If you are curious about Linq to NHibernate and want to quickly check it out, here is an easy way to do it.

Check out a copy of the CodeCampServer source (svn).  CodeCampServer is easy to work with because it has good tests on an already-working NHibernate implementation.

Check out a copy of NH Contrib (svn), open the NHibernate.Linq solution in Visual Studio and build. You may have to remove the compiler flag that indicates .NET 3.5 sp1 is available.

Open the CodeCampServer.sln and remove the NHibernate.dll reference from the DataAccess and IntegrationTests projects.  Add a reference to NHibernate.dll from the NHibernate.Linq bin directory.  Build and run tests.  I had to play with the DatabaseTesterBase.recreateDatabase() method because I think are are still some bugs in Export():

public static void recreateDatabase()
{
    var exporter = new SchemaExport(new HybridSessionBuilder().GetConfiguration());
    exporter.Drop(false, true);
    exporter.Create(false, true);
}

Now add a reference to NHibernate.Linq.dll in the DataAccess project. You can start writing queries and check your work with the integration tests that already exist. Here are a few simple queries for ConferenceRepository:

public Conference[] GetAllConferences()
{
    ISession session = getSession();
    var query = from conference in session.Linq<Conference>() select conference;
    return query.ToList().ToArray();
}

public Conference GetConferenceByKey(string key)
{
    ISession session = getSession();
    var query = from conference in session.Linq<Conference>()
                where conference.Key == key
                select conference;
    return query.Distinct().Single();
}

public Conference GetFirstConferenceAfterDate(DateTime date)
{
    ISession session = getSession();
    var query = from conference in session.Linq<Conference>()
                where conference.StartDate >= date
                orderby conference.StartDate
                select conference;
    return query.First();
}

public Conference GetMostRecentConference(DateTime date)
{
    ISession session = getSession();
    var query = from conference in session.Linq<Conference>()
            where conference.EndDate <= date
            select conference;
    query.OrderByDescending(conference => conference.EndDate);
    return query.First();
}

About these ads
This entry was posted in Tools and tagged , , . Bookmark the permalink.

9 Responses to Linq to NHibernate in 10 minutes

  1. Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #142

  2. Pingback: Dew Drop - July 23, 2008 | Alvin Ashcraft's Morning Dew

  3. Phil Webb says:

    public Conference GetMostRecentConference(DateTime date)
    {
    ISession session = getSession();
    var query = from conference in session.Linq()
    where conference.EndDate conference.EndDate);
    return query.First();
    }

    The above doen’t work as expected. It returns 2004 – from conferencerepositorytester.

    Actually needs the following:

    public Conference GetMostRecentConference(DateTime date)
    {
    ISession session = getSession();
    var query = from conference in session.Linq()
    where conference.EndDate conference.EndDate).First();
    }

    This returns 2005. Subtle difference that has burnt me before.

  4. Phil Webb says:

    OOPS the pasted code has some stuff gone missing – probably the matching angle brackets.

    The return from the second function needs to be:

    return query.OrderByDescending(conference => conference.EndDate).First();

    replacing:

    query.OrderByDescending(conference => conference.EndDate);
    return query.First();

    Lets see how that goes

  5. Pingback: So you want to learn NHibernate? (or, NHibernate Hyperlink Acupuncture) | The Freak Parade

  6. Pingback: kjempekjekt.com » Blog Archive » Fem vekttall NHibernate

  7. mohsen says:

    can u give me username and password for connecting to http://codecampserver.googlecode.com/svn/trunk/ ?

  8. Ryan Vice says:

    This took me 12 minutes :/

    JK, Thanks for another great article Matt!

  9. Artur says:

    Your provider does not support extensibility
    That is so sad =((

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s