Using the new Linq to NH Provider and migrating from the old one

Using the new Linq provider is pretty simple. It all hangs of a Query() extension method on ISession, so you can do things like the following:

  from c in session.Query<Customer>() select c

In my tests, I've tended to wrap the session.Query() call behind a simple facade, along the lines of:

  public class Northwind
  {
   private readonly ISession _session;

   public Northwind(ISession session)
   {
   _session = session;
   }

   public IQueryable<Customer> Customers   { get { return _session.Query<Customer>(); }
  }

Of course, that's entirely optional, but I find the resulting code easier to read:

  from c in db.Customers select c

Once you know how to hook into the session (which as you can see is pretty simple), the rest is just straightforward Linq code, and entirely up to you! Right now I'm not exposing any extension points, but they'll be coming soon (plus another post to describe how to use them).

The version 1 provider used an ISession extension method call Linq() to provide its hook. I purposefully used a different name, since there's no reason at all why you can't use both providers within the same project or, indeed, within the same session. So that gives a couple of migration options for folk that want to move to the new provider:

  • Leave all your current queries using .Linq(), and start using .Query() for new ones.

  • Start changing existing queries from .Linq() to .Query(), just by changing them or by a simple search & replace. The rest of the expression will (hopefully!) just work.

  • Drop your reference to the original Linq provider assembly, and create your own extension method:

      public static IQueryable<T> Linq<T>(this ISession session)
      {
        return session.Query<T>();
      }


    This lets you switch to the new provider without changing a line of your code - and if you find it all goes to hell, you just re-add the V1 reference and comment out your extension method. Should work like a treat.

Other than that, I don't think there's much to tell - usage really should be pretty simple. Oh, one thing that springs to mind - although you can use the V1 provider and the new provider within the same project (or session), don't try to compose queries from them together; that's really going to do weird stuff!

Comments

#  Twitter Trackbacks for Using the new Linq to NH Provider and migrating from the old one [imeta.co.uk] on Topsy.com
Gravatar
Twitter Trackbacks for

Using the new Linq to NH Provider and migrating from the old one
[imeta.co.uk]
on Topsy.com
Left by Pingback/TrackBack on 12/17/2009 12:01 AM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar And, perhaps, we can push people to implements a real IRepository<T>
fabiomaulo.blogspot.com/.../...dao-repository.html
Left by Fabio Maulo on 12/17/2009 12:10 AM
# The Morning Brew - Chris Alcock &amp;raquo; The Morning Brew #500
Gravatar The Morning Brew - Chris Alcock &amp;raquo; The Morning Brew #500
Left by Pingback/TrackBack on 12/17/2009 9:49 AM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Can I tell Linq to eagerly load the associated collections? E.g. I have lazy specified in the mapping but I want eager for this particular query?
Left by ulu on 12/17/2009 6:49 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Hi, Steve!
Where can we get your provider?
Do you know if it supports ASP.NET Dynamic Data?
Thanks,
RP
Left by Ricardo Peres on 12/17/2009 8:02 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Ulu,

There's no support yet for choosing between eager & lazy loading; the query will run in accordance with your mapping definitions. This is planned however, so watch this space!

Cheers,

Steve
Left by Steve on 12/17/2009 11:45 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Ricardo,

The provider is part of the trunk, so either grab the source & compile, or head over to www.hornget.net and grab it from there.

I've not tried Dynamic Data against the new provider, but I see no reason why it shouldn't work. If you try it out, be sure to let me know how it goes!

Cheers,

Steve
Left by Steve on 12/17/2009 11:46 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Just a reminder that I'm heading off on holiday and will likely have no internet access for the next couple of weeks - comments on this blog are moderated, so please continue leaving them, but don't be upset if they don't show (or get responses) for a week or two.

Cheers,

Steve
Left by Steve on 12/17/2009 11:48 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Hi Steve,
excellent work!

I haven't tried the trunk yet (just reading articles) but I assume it is NH version 3.0.

I guess there would be a problem to get FluentNhibernate running with NH trunk, so any estimates will we get something like "beta NH3.0" so FNH can jump and support it?
Left by cowgaR on 1/5/2010 11:15 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar cowgaR,

I'm not sure what the timescales are for NH3.0, but in the meantime you should be able to download a compatible set of NH & FNH binaries from Hornget - check out www.hornget.net/.../fluentnhibernate-trunk
Left by Steve on 1/7/2010 5:10 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Great job.
1. When will subqueries be supported ?
I have created a query againts Order with Orderlines and return them as OrderDTo and OrderLineDto, but this projection won't work yet. I guess it's not implemented.

2. What happened to Expand("..") ?
How do I eagerly load now ?
Left by Lars-Erik on 1/31/2010 8:24 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar When will it support subqueries ?
Left by Lars-Erik on 2/1/2010 9:41 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar I cannot access hornget to download. Any other link?
Left by irwan on 2/25/2010 6:59 AM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar Lars, Subqueries are planned, but I'm not going to commit to a date just yet :) Of course, if you can return your mapped entities rather than projected DTOs, then they already work.

As far as Expand goes, I'm working on that right now, hope to have something out real soon
Left by sstrong on 2/25/2010 12:23 PM
# re: Using the new Linq to NH Provider and migrating from the old one
Gravatar irwan, Sorry, but looks like Horn has gone awol for a few days (a host at Amazon failed and took out the server - the Horn guys are rebuilding it so it should hopefully be back soon). In it's absence, I don't know of any other places where you can get binaries. You can, of course, just get the latest trunk and build it yourself.
Left by sstrong on 2/25/2010 12:24 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

Please add 3 and 1 and type the answer here:

Preview Your Comment.