Sunday, November 8, 2009

LINQ to NHibernate Tutorial

Note: This post applies to NHibernate 2.1 with LINQ to NHibernate version 1.0.

Starting with LINQ to NHibernate is really simple; you just have to add a reference to NHibernate.Linq.dll to your host project (a test project, an ASP.NET app).

Here is a unit test to demonstrate a simple LINQ query.  This test uses the same entities defined in my previous NHibernate tutorial.

[Test]
public void SelectWithPredicate()
{
    using (var session = _factory.OpenSession())
    {
        //Act
        var customers = from cust in session.Linq<Customer>()
                        where cust.LastName.StartsWith("St")
                        select cust;
        //Assert
        Assert.AreEqual(1, customers.Count());
    }
}

The Linq() method is an extension method defined in NHibernate.Linq.dll. It provides the entry point to execute Linq queries on your NHibernate entities. When you run that test, NHibernate generates the following statement:

SELECT count(*) as y0_ FROM Customer this_
WHERE this_.LastName like @p0;@p0 = 'St%'

In an upcoming post I will explore the capabilities and limitations of LINQ to NHibernate. For now, here is the full test class I used to discover LINQ to NHibernate:

using System.Diagnostics;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using NUnit.Framework;
using PocoLib;

namespace PocoLibTests
{
    [TestFixture]
    public class CustomerLINQTests
    {
        private ISessionFactory _factory;

        [SetUp]
        public void Setup()
        {
            Configuration configuration = new Configuration();
            configuration.Configure("PocoLib.cfg.xml");
            configuration.AddXmlFile("Customer.hbm.xml");

            _factory = configuration.BuildSessionFactory();

            SetupDatabase();
        }

        [TearDown]
        public void TearDown()
        {
            PurgeDatabase();
        }

        [Test]
        public void SelectAllCustomers()
        {
            using (var session = _factory.OpenSession())
            {
                //Act
                var customers = from cust in session.Linq<Customer>()
                                select cust;
                //Assert
                Assert.AreNotEqual(4, customers.Count());
            }
        }

        [Test]
        public void SelectWithPredicate()
        {
            using (var session = _factory.OpenSession())
            {
                //Act
                var customers = from cust in session.Linq<Customer>()
                                where cust.LastName.StartsWith("St")
                                select cust;
                //Assert
                Assert.AreEqual(1, customers.Count());
            }
        }

        private void SetupDatabase()
        {
            using (var session = _factory.OpenSession())
            {
                Customer c = new Customer();
                c.FirstName = "Mike";
                c.LastName = "Laroco";
                c.Address.Country = "USA";
                session.SaveOrUpdate(c);

                c = new Customer();
                c.FirstName = "Bubba";
                c.LastName = "Stuart";
                c.Address.Country = "USA";
                c.Address.State = "Florida";
                session.SaveOrUpdate(c);

                c = new Customer();
                c.FirstName = "Ricky";
                c.LastName = "Carmichael";
                c.Address.Country = "USA";
                c.Address.State = "Florida";
                session.SaveOrUpdate(c);

                c = new Customer();
                c.FirstName = "Jean-Sebastien";
                c.LastName = "Roy";
                c.Address.Country = "Canada";
                c.Address.State = "Quebec";
                session.SaveOrUpdate(c);

                session.Flush();
            }

            Debug.WriteLine("end prepare data");

        }

        private void PurgeDatabase()
        {
            using (var session = _factory.OpenSession())
            {
                session.Delete("from Customer");
                session.Flush();
            }
        }

    }
}

Thanks

3 comments:

  1. Thank you very very much .. very easy to understand. I really appreciate the time you spent in writing the tutorial

    ReplyDelete