I’ve been an NHibernate fan for a long time, but on my recent projects I’m making the switch to Entity Framework.
Although I like the overall experience, there are still lot of features missing.
One of the things I liked about NHibernate was the support for query batching using the NHibernate Future<> syntax.
This allowed you to write multiple simpler queries instead of writing one complex one. The moment you try to read one query result, all batched queries are sent in 1 command(!) to the database and the results are returned.
// prepare the query | |
var query = session.CreateQuery("from Person p where p.Id = :id") | |
.SetParameter("id", person.Id) | |
.Future<Person>(); | |
// eager load first collection | |
session.CreateQuery("from Person p | |
left join fetch p.Addresses a where p.Id = :id") | |
.SetParameter("id", person.Id) | |
.Future<Person>(); | |
// eager load second collection | |
session.CreateQuery("from Person p | |
left join fetch p.Phones ph | |
left join fetch ph.PhoneType pt where p.Id = :id") | |
.SetParameter("id", person.Id) | |
.Future<Person>(); |
Although a similar feature does not exist in Entity Framework out-of-the-box, you can use the EntityFramework.Extended library to get this functionality. This library extends the functionality of Entity Framework with the following features:
In this case I’m looking at the Future Queries feature:
// build up queries | |
var q1 = db.Users | |
.Where(t => t.EmailAddress == "one@test.com") | |
.Future(); | |
var q2 = db.Tasks | |
.Where(t => t.Summary == "Test") | |
.Future(); | |
// this triggers the loading of all the future queries | |
var users = q1.ToList(); |