Linq to NHibernate

The popularity of NHibernate is steadily increasing. At the same time people get used to LINQ. Now there exists a LINQ to NHibernate provider since quite some time. It's not a complete implementation of a LINQ provider but it is still quite useful. Most of the day to day problems we face when developing typical business application can be solved by using this provider. And if there is a query that cannot be executed against the provider we still have the option to falling back to the hibernate query language (HQL).

In this post I'll give you an introduction on how you can use NHibernate in conjunction with LINQ. Let's start with the definition of a domain model we are going to use when querying the database by using LINQ expressions or LINQ query operators.

The domain model

I have the following simple domain model. A person can have a set of assigned tasks.

model

The entities

I want to keep the entities as simple as possible for this example. So their implementation is as follows

public class Person
{
    public Person()
    {
        Tasks = new List<Task>();
    }
 
    public virtual long Id { get; set; }
    public virtual string Lastname { get; set; }
    public virtual string Firstname { get; set; }
    public virtual IList<Task> Tasks { get; set; }
}
 
public class Task
{
    public virtual long Id { get; set; }
    public virtual string TaskName { get; set; }
    public virtual DateTime DueDate { get; set; }
}

one thing worth noting is the fact that I instantiate a new empty Tasks collection in the constructor of the Person class.

The mapping

For the mapping of my domain model I am going to use the Fluent NHibernate framework (please refer to my previous posts for a detailed introduction to this framework: part 1, part 2, part 3, part 4) .

The Task is very easy to map

public class TaskMap : ClassMap<Task>
{
    public TaskMap()
    {
        Id(x => x.Id);
        Map(x => x.TaskName);
        Map(x => x.DueDate);
    }
}

And the person is nothing more complicated

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id);
        Map(x => x.Firstname);
        Map(x => x.Lastname);
        HasMany<Task>(x => x.Tasks)
            .LazyLoad()
            .Cascade.All();
    }
}

Just let's have a look at the mapping of the Tasks collection. I want the tasks to only be lazy loaded when loading a person entity (LazyLoad). And I want NHibernate to automatically save or update any tasks associated with a given person if ever the person is updated (Cascade.All).

Test the mapping

One quick test I want to always do is to check whether my mappings work as expected. Since I use Fluent NHibernate this is very easy and straight forward. A lot of infrastructure code is available to me to leverage and thus simplify my unit test regarding mapping of the domain entities.

The base test fixture

First I want to present my base class I use for all of my tests. Every test fixture used in this post will inherit from this base class.

public class FixtureBase<TModel> where TModel : PersistenceModel, new()
{
    protected SessionSource SessionSource { get; set; }
    protected ISession Session { get; private set; }
 
    [SetUp]
    public void SetupContext()
    {
        var cfg = new SQLiteConfiguration()
                        .InMemory()
                        .ShowSql();
        SessionSource = new SessionSource(cfg.ToProperties(), new TModel());
        Session = SessionSource.CreateSession();
        SessionSource.BuildSchema(Session);
        
        Context();
        Because();
 
        Session.Flush();
        Session.Clear();
    }
 
    [TearDown]
    public void TearDown()
    {
        TearDownContext();
 
        Session.Close();
        Session.Dispose();
    }
 
    protected virtual void Context()
    {
    }
 
    protected virtual void Because()
    {
    }
 
    protected virtual void TearDownContext()
    {
    }
}

First of all my base fixture has a generic parameter TModel. TModel represents the persistence model used during the tests. The persistence model must inherit from the PersistenceModel (provided by Fluent NHibernate) and it must have a default constructor.

In the SetupContext method (which is executed before each test)

  • I define my configuration I want to use. Most of the time I use the SQLite database in in-memory mode for my database related tests. Fluent NHibernate provides me such a configuration object. For debugging purposes I declare that I want to see the SQL statements generated by NHibernate (ShowSql).
  • Second I create a session source and pass the above configuration as well as an instance of the model used to the constructor of the session source.
  • Then I create a new session object. Note that since I am using SQLite in in-memory mode I have to use the same session object during the whole test (including setup and tear down) since the database schema (and the data) is destroyed when the session is closed.
  • I now use the Session object created above to generate the database schema. The schema is generated from the information provided by the model (which consists of all mappings)
  • Now I call the virtual Context and Because methods. The reason is that I want my unit test to be more BDD like
  • Finally I flush all pending operations from the session to the database and then clear the session.

The persistence model

The persistence model I am using is very straight forward. I just include all class mappings that are in the same assembly as the PersonMap.

public class TestModel : PersistenceModel
{
    public TestModel()
    {
        addMappingsFromAssembly(typeof(PersonMap).Assembly);
    }
}

I could also explicitly add mappings if I want so

public class TestModel : PersistenceModel
{
    public TestModel()
    {
        addMapping(new PersonMap());
        addMapping(new TaskMap());
    }
}

The test

Writing a test for the mapping is really straight forward with the aid of the PersistenceSpecification class provided by Fluent NH.

[Test]
public void can_add_person_without_tasks()
{
    new PersistenceSpecification<Person>(Session)
        .CheckProperty(x => x.Firstname, "Gabriel")
        .CheckProperty(x => x.Lastname, "Schenker")
        .VerifyTheMappings();
}

or a test which also tries to add tasks

[Test]
public void can_add_person_with_tasks()
{
    var tasks = new[]
                    {
                        new Task {TaskName = "Task 1", DueDate = DateTime.Today.AddDays(5)},
                        new Task {TaskName = "Task 2", DueDate = DateTime.Today.AddDays(6)},
                        new Task {TaskName = "Task 3", DueDate = DateTime.Today.AddDays(3)},
                    };
    new PersistenceSpecification<Person>(Session)
        .CheckProperty(x => x.Firstname, "Gabriel")
        .CheckProperty(x => x.Lastname, "Schenker")
        .CheckList(x=>x.Tasks, tasks)
        .VerifyTheMappings();
}

Do I need to tell that the tests pass successfully? It's really that easy... I have not needed a single line of XML so far. And no "magic strings" are involved. For me it's really a joy to work like this.

And now finally we get LINQ into the play

The context

In LINQ the notion of a context is very important. It represent kind of a facade to the database. LINQ to NHibernate provides us a base class from which we can derive when we define our own context. In our simplified model we have only 2 entities and thus our context needs just two members to have access via LINQ to either the persons or tasks.

public class SampleContext : NHibernateContext
{
    public SampleContext(ISession session)
        : base(session)
    { }
 
    public IOrderedQueryable<Person> Persons
    {
        get { return Session.Linq<Person>(); }
    }
 
    public IOrderedQueryable<Task> Tasks
    {
        get { return Session.Linq<Task>(); }
    }
}

Testing

Now I want to implement a first test to check whether LINQ to NHibernate does indeed work as expected. First I have to setup a context where I have some person object with tasks in the database

public class a_repository_with_persons_having_tasks : Person_Fixture
{
    protected Person[] persons;
    private IList<Task> tasks1, tasks2, tasks3;
 
    protected override void Context()
    {
        base.Context();
        tasks1 = new[]
                     {
                         new Task {TaskName = "Task 1.1", DueDate = DateTime.Today.AddDays(5)},
                         new Task {TaskName = "Task 1.2", DueDate = DateTime.Today.AddDays(6)},
                         new Task {TaskName = "Task 1.3", DueDate = DateTime.Today.AddDays(3)},
                     };
        tasks2 = new[]
                     {
                         new Task {TaskName = "Task 2.1", DueDate = DateTime.Today.AddDays(5)},
                         new Task {TaskName = "Task 2.2", DueDate = DateTime.Today.AddDays(6)},
                         new Task {TaskName = "Task 2.3", DueDate = DateTime.Today.AddDays(3)},
                     };
        tasks3 = new[]
                     {
                         new Task {TaskName = "Task 3.1", DueDate = DateTime.Today.AddDays(5)},
                         new Task {TaskName = "Task 3.2", DueDate = DateTime.Today.AddDays(6)},
                         new Task {TaskName = "Task 3.3", DueDate = DateTime.Today.AddDays(2)},
                     };
        persons = new[]
                      {
                          new Person {Firstname = "Gabriel", Lastname = "Schenker", Tasks = tasks1},
                          new Person {Firstname = "John", Lastname = "Doe", Tasks = tasks2},
                          new Person {Firstname = "Ann", Lastname = "Moe", Tasks = tasks3},
                      };
        foreach (var person in persons)
            Session.Save(person);
    }
}

Now I can write a test for the case where I want to retrieve all persons from the database

[TestFixture]
public class when_querying_all_persons : a_repository_with_persons_having_tasks
{
    private IEnumerable<Person> list;
 
    protected override void Because()
    {
        list = from p in db.Persons
               select p;
    }
 
    [Test]
    public void should_return_all_persons()
    {
        list.Count().ShouldEqual(persons.Length);
    }
}

The query generated by the above test is

SELECT  this_.Id as Id0_0_, 
        this_.Lastname as Lastname0_0_, 
        this_.Firstname as Firstname0_0_ 
FROM [Person] this_

That's exactly what we were expecting!

Using Where to get a filtered list

Now let's do some more interesting stuff. I want to have a filtered list of persons

[TestFixture]
public class when_retrieving_filtered_list_of_persons : a_repository_with_persons_having_tasks
{
    [Test]
    public void can_filter_by_LastName()
    {
        var list = db.Persons.Where(x=>x.Lastname=="Doe");
        list.Count().ShouldEqual(1);
    }
 
    [Test]
    public void can_filter_by_task()
    {
        var list = from p in db.Persons
                   from t in p.Tasks
                   where t.DueDate == DateTime.Today.AddDays(3)
                   select p;
        list.Count().ShouldEqual(2);
    }
}

In the first test I use the Where extension method defined by LINQ to filter my collection of persons. The extension methods defined by LINQ are also called query operators. The select statement generated by Linq2NH is

SELECT count(*) as y0_ 
FROM [Person] this_ 
WHERE this_.Lastname = @p0; 
@p0 = 'Doe'

In the second test I use the new language extension introduced for C# (also called a query expression) to get a filtered set of persons. This time we get the following select statement sent to the database

SELECT          count(*) as y0_ 
FROM            [Person] this_ 
left outer join [Task] t1_ on this_.Id=t1_.Person_id 
WHERE t1_.DueDate = @p0; 
@p0 = '29.11.2008 00:00:00'

Obviously also joins between tables work as expected.

Getting ordered lists

Often we need an ordered list of items, in this case persons ordered by their last name.

[Test]
public void can_order_by_LastName()
{
    var list = db.Persons.OrderBy(x => x.Lastname);
    list.First().Lastname.ShouldEqual("Doe");
}

The query generated is

SELECT  this_.Id as Id0_0_, 
        this_.Lastname as Lastname0_0_, 
        this_.Firstname as Firstname0_0_ 
FROM [Person] this_ 
ORDER BY this_.Lastname asc

Code

The code accompanying this post can be found here.

Summary

It is straight forward to use LINQ to NHibernate. Although the Linq2NH provider is not fully implemented it is more than sufficient for most scenarios we encounter in typical projects. And if ever we encounter a query that cannot be executed through the Linq2NH provider we can still implement the query by using HQL instead.

GabrielEnjoy

 Blog Signature Gabriel

Print | posted on Wednesday, November 26, 2008 5:13 AM

Comments on this post

# re: Linq to NHibernate

Requesting Gravatar...
Very cool article.
I have a couple of questions:
What are the limitations of LinqToNH as today?
What is pending to be done?
Is there a milestone to reach for this project?
Where can I download the required assemblies to start to play with it?
Best regards.
Left by Jaime Febres on Nov 27, 2008 4:44 AM

# re: Linq to NHibernate

Requesting Gravatar...
@Jaime:
- Linq2NH has the same limitations as the criteria api of NH.
- There is a plan to totally re-implement the provider and provide a AST (abstract syntax tree) based implementation. Currently Ayende is raising funds for this project. If you are interested in supporting this project please contact Ayende.
- Linq2NH is a project of the NH contributions (but to start with - it is included in the binaries of Fluent NHibernate)
Left by Gabriel N. Schenker on Nov 27, 2008 10:47 PM

# re: Linq to NHibernate

Requesting Gravatar...
Jaime: I've heard that there are 2 implementation of LINQ 2 NH.

But (can't remember where I read it) they said both are already avaiable, so with your mentioned plan to release Linq2NH with AST implementation I would count 3 of them already ;)

I am lost.

And why is it such a problem to have LINQ provider working (I know it isn't breeze but it isn't nuclear science as well) when most of the OR/M tools already have it working nicely, from LLBLGen to Lightspeed to (partialy) working Subsonic 3.0, and some of those projects aren't enterprise oriented as NHibernate is.

It is surely lacking behind, Oren allways tried to keep NHibernate somewhat _hacker_ tool and it still don't have a LINQ provider.
Left by nefajciar on Dec 04, 2008 4:06 AM

# re: Linq to NHibernate

Requesting Gravatar...
@nefajciar: It *IS* hard to implement a LINQ provider which covers 100% of the possible queries. It was not that hard to implement one that "only" covered the most important part (and not all edge cases). This one is available since quite some time now as one of the NHibernate contribution projects. This one is also the ONLY "official" implementation so far.
Left by Gabriel N. Schenker on Dec 07, 2008 6:39 PM

# re: Linq to NHibernate

Requesting Gravatar...
All of it seems very nice, but I have to ask if you know how well-tested it is? Is it something to put in production code and do you know if the join-method is implemented?
Left by Mats Nilsson on Dec 20, 2008 6:24 AM

# re: Linq to NHibernate

Requesting Gravatar...
@Mats: NH2Linq is covered by a large number of unit tests. They represent the typical scenarios one encounters when querying. Most of the tests pass. But some (rather specific) features are not yet supported and thus the respective tests fail.
And yes, you can definitely use it for production code! A lot of people do.
Left by Gabriel N. Schenker on Dec 22, 2008 6:38 PM

# re: Linq to NHibernate

Requesting Gravatar...
Hi Gabriel

I found that the test "can_filter_by_task()" is not working as expected.

If you see thro' VS IDE, watch window shows following error :
could not instantiate: <>f__AnonymousType0`2[[Linq2NH.Person, Linq2NH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Linq2NH.Task, Linq2NH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Watch is added on following statement -
var list = from p in db.Persons
from t in p.Tasks
where t.DueDate == DateTime.Today.AddDays(3)
select p;

Is such statement is supported by NHibernate.LINQ ??

[Please not that, to debug it in VS2008, I changed reference of Nunit to VS's test tool namespace and updated attrib for same.


Regards
Rajesh

Left by Rajesh on Mar 06, 2009 3:30 AM

# re: Linq to NHibernate

Requesting Gravatar...
After the following modification test stopped working:
[Test]
public void can_filter_by_task_with_list()
{
var list = from p in db.Persons
from t in p.Tasks
where t.DueDate == DateTime.Today.AddDays(3)
select p;

list.ToList().Count().ShouldEqual(1);
}

NHibernate: SELECT this_.Id as Id1_1_, this_.Lastname as Lastname1_1_, this_.Firstname as Firstname1_1_, t1_.Person_id as Person4_3_, t1_.Id as Id3_, t1_.Id as Id0_0_, t1_.DueDate as DueDate0_0_, t1_.TaskName as TaskName0_0_ FROM [Person] this_ left outer join [Task] t1_ on this_.Id=t1_.Person_id WHERE t1_.DueDate = @p0; @p0 = '4/19/2009 12:00:00 AM'

failed: NHibernate.QueryException : could not instantiate: <>f__AnonymousType0`2[[Linq2NH.Person, Linq2NH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Linq2NH.Task, Linq2NH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
----> System.InvalidCastException : Object must implement IConvertible.

Is it one of the limitations you talked about?
Left by Andrey T. on Apr 15, 2009 4:26 PM

# re: Linq to NHibernate

Requesting Gravatar...
"Note that since I am using SQLite in in-memory mode I have to use the same session object during the whole test (including setup and tear down) since the database schema (and the data) is destroyed when the session is closed."

Thanks this just saved me a big headache.. :)
Left by Jake Scott on Jun 30, 2009 9:31 PM

# re: Linq to NHibernate

Requesting Gravatar...
Doesn't even work.

WHY can't you NH people EVER post a single working line of code?

Instead of blathering on uselessly, POST A WORKING SAMPLE WITH EVERYTHING PEOPLE NEED TO SEE for ONCE in your life.
Left by Doesn't even work on Sep 13, 2009 7:16 AM

# re: Linq to NHibernate

Requesting Gravatar...
Thanks for all the great posts. You write about difficult subjects in such a clear manner that even I can understand them!!
Left by Multivariate Testing on Nov 07, 2009 12:23 PM

# re: Linq to NHibernate

Requesting Gravatar...


It’s a debatable readability improvement, but you can treat each where condition separately, rather than ANDing them together. It’s sometimes more readable, and definitely makes it easier to add and remove lines in the future:

where call.StartTime > beginDate
where (call.EndTime == null || call.EndTime < DateTime.Now) // Reordered to put the null check in front, just in case the query is ever run against in-memory data
where call.Interpreter.Id == interpreterId
Left by Call Center Software on Nov 07, 2009 12:38 PM

# re: Linq to NHibernate

Requesting Gravatar...
Leaving the readability aspect aside, the linq query is strongly typed, while the criteria one isn’t. That’s also a big difference.
Left by International Medical Insurance on Nov 07, 2009 12:54 PM

# re: Linq to NHibernate

Requesting Gravatar...
I know that many advanced programmers strongly believe in layering their code, but isn't this getting a bit ridiculous? I mean, adding yet another layer between your business logic/model and your database. Putting NHibernate or LINQ in between the model and DB makes sense (because the simplify the coding when used right), but putting both of them in between just adds unnecessary complexity, doesn't it?
Left by Electric Tea Kettle on Nov 08, 2009 1:12 AM

# re: Linq to NHibernate

Requesting Gravatar...
Putting NHibernate or LINQ in between the model and DB makes sense (because the simplify the coding when used right), but putting both of them in between just adds unnecessary complexity, doesn't it?
Left by Orkut Greetings on Nov 16, 2009 10:46 AM

# re: Linq to NHibernate

Requesting Gravatar...
It is recommended article on Linq to Hibernate, but I think NHibernate is much better than it. Thanks for sharing the code.
Left by Prada Eyeglasses on Nov 20, 2009 10:44 AM

# re: Linq to NHibernate

Requesting Gravatar...
Language Integrated Query is an advanced concept introduced in .Net 3.5.Using this you can write your database query in .Net Environment itself.You can read and update the database very effectively using Linq.
Left by SEO on Dec 03, 2009 6:34 PM

# re: Linq to NHibernate

Requesting Gravatar...
I am using SQLite in in-memory mode I have to use the same session object during the whole test (including setup and tear down) since the database schema (and the data) is destroyed when the session is closed."
Left by como aprender a jugar poker on Dec 03, 2009 8:25 PM

# re: Linq to NHibernate

Requesting Gravatar...
The post is very nicely written about NHibernate.I always love to read this kind of stuff.Thanks for sharing this post.
Left by Edmonton Web Design on Dec 07, 2009 7:05 AM

# finding cell number

Requesting Gravatar...
You write about difficult subjects in such a clear manner that even I can understand them!!
Left by finding cell number on Dec 07, 2009 5:50 PM

# re: Linq to NHibernate

Requesting Gravatar...
NHibernate is indeed a very good application. Codes and the domain model are very helpful. Thanks for sharing.
Left by Limousine NY on Dec 08, 2009 3:44 PM

# re: Linq to NHibernate

Requesting Gravatar...
Before diving into the code it is essential to define what LINQ actually is. LINQ is not C# 3.0, and vice versa. LINQ relies heavily on the new language enhancements introduced in C# 3.0;
Left by flooring on Dec 11, 2009 4:57 PM

# re: Linq to NHibernate

Requesting Gravatar...
Great code that will come in use soon for my new project. Thanks
Left by shock collars for dogs on Dec 20, 2009 8:39 AM

# re: Linq to NHibernate

Requesting Gravatar...
this si great psot! thanks
Left by cable glossary on Dec 21, 2009 7:00 PM

# re: Linq to NHibernate

Requesting Gravatar...
Is such statement is supported by NHibernate.LINQ ??

[Please not that, to debug it in VS2008, I changed reference of Nunit to VS's test tool namespace and updated attrib for same.
Left by pandora jewelry on Dec 24, 2009 11:48 PM

# re: Linq to NHibernate

Requesting Gravatar...
I had a great time reading around your post as I read it extensively. Excellent writing! I am looking forward to hearing more from you.
Left by Internet Kasino Test on Dec 25, 2009 9:31 PM

# re: Linq to NHibernate

Requesting Gravatar...
I must say, greatest article tips!
Left by Purchase essay on Dec 30, 2009 8:02 AM

# re: Linq to NHibernate

Requesting Gravatar...
I agree with author, anyways great articlei ndeed, I really like the idea of the writer, keep up the good work, cheersThanks for this
information.
Left by airport bus New York on Jan 14, 2010 3:24 PM

# re: Linq to NHibernate

Requesting Gravatar...
Putting NHibernate or LINQ in between the model and DB makes sense (because the simplify the coding when used right), but putting both of them in between just adds unnecessary complexity, doesn't it?
Left by web design nyc on Jan 14, 2010 7:54 PM

# re: Linq to NHibernate

Requesting Gravatar...
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful.
Left by Caviar on Jan 15, 2010 2:47 PM

# re: Linq to NHibernate

Requesting Gravatar...
I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharinging
Left by Movers Bronx on Jan 15, 2010 5:45 PM

# re: Linq to NHibernate

Requesting Gravatar...
oh ok i agree
Left by make money online on Jan 16, 2010 1:20 PM

# re: Linq to NHibernate

Requesting Gravatar...
I will immediately grab your rss feed to stay privy of any updates. Pleasant work and much success in your business dealings!
Left by healthy weight loss pills on Jan 17, 2010 9:09 AM

# re: Linq to NHibernate

Requesting Gravatar...
The is the first model ive seen for linking the two. I will try the code.
Left by fellowes powershred on Jan 18, 2010 8:38 AM

# re: Linq to NHibernate

Requesting Gravatar...

Lot of thanks for this article. Its really a very good topic. Its so interesting and attractive. I like it so much.

Thanks.
Left by Brochure Printing on Jan 27, 2010 4:49 PM

# re: Linq to NHibernate

Requesting Gravatar...
What's more, a bracelet can also be equipped with different small pandora jewelry and even you can change it according to your mood at any time. Here are some meanings of pendant. Small Plane stands for traveling and adventure ; anchor, stability and hope; your baby's boots, having a lot of babies; small feeding pandora bracelets abundant food; Church means happiness and stability of marriage; dragonfly means riches; Eiffel Tower means travel and exploration; four-leaf clover means fortune; horseshoe means luck; Nest means a happy family; bride shows a happy bride in her coming pandora jewelleryship steering shows pandora beadscalming and confidence; pandora ukcoin shows rich marriage life. Wish bone, dreams being about to come true; pandora charm bracelets, love; one heart shot by an arrow, romantic love; purse, wealth; and heart-shaped lock, true love.
Left by kasandra1972 on Jan 29, 2010 12:51 PM

Your comment:

 (will show your gravatar)
 
Please add 5 and 3 and type the answer here: