NHibernate Query Generator 1.5 Is Done! [Time With Magic]
I have been working (and blogging about it) for a while, and I think that I finally hit the right combination of keys to make it compile, so I am shipping it :-)
If you fail to recall, it started as a simple way to generate criteria queries in strongly typed manner. The first version didn't do much, and was fairly simple. As soon as I started using it, I began to want more, much more. If I was working with an open compiler (Boo), I could have written this as an extentions and be done with it. Unfortanetly, I am not :-(, so I had to resort to a vile trickery and back handed knavery in order to make csc.exe think that I am doing something valid.
At its basis, NHibernate Query Generator (from now on, NQG) is a code generator that works on a set of Hibernate Mapping Files (*.hbm.xml) or an Active Record assemblies. It spits out code that will allow you to perform OO queries against your database. I have posted about my progress several times in the past, but let us go over what it gives you.
- This is the most basic example:
Where.User.Name == "Ayende"
- Then there is a nice way to do multiply queries in a single statement, like this:
Where.Comment.Content == "Active Record Rocks!" ||
Where.Comment.Content.Like("NHibernate", MatchMode.Anywhere) - But once you have this, you start to want more. Automatic handling of joins between tables, like this:
Where.Post.Blog.Name == "Ayende @ Blog"
- And if we have a single level of joins, why not several?
Where.Post.Blog.Author.Name == "Ayende"
- As long as we are at this, why are we querying against simple properties only? We can query against objects, like this sample:
Where.Comment.Post == post
- And then you get stuck on the ternary logic of SQL and you want to have OO view of nulls, like this, which perform exactly as you think it should in C# and translated to "is not null" on the DB side:
Where.Post.Blog != null
- Gettting Compiler Errors for some invalid queries was a lot harder though, but this code is illegal and will be caught at compile time:
Where.Post.Blog.Author == ayende && Where.Blog.Name == "Ayende"
- As long as we are taking care of querying, what about orderring? Can't forget that, can we?
User.FindAll(OrderBy.User.Name)
Extending the queries:
The generated classes are based on your mapping, but all the generated classes are marked partial so you can extend them with your own queries:
User.FindAll(Where.User.HasMoreThanThousandPosts)
Other features:
- NQG supports C# and VB.Net
- It can work in two modes: command line tool, which can be run as part of the build or a custom tool for Visual Stuido that will generate code as you edit your mapping.
- It can work on Hibernate Mapping Files or Active Record Assemblies.
-
Rich querying capabilities - above I showed just the showy stuff, but you got Like(), In(), and just about everything that you can think about.
-
It looks amazingly like real sentences in VB.Net
How to Use:
- From the command line:
"NHQG cs *.hbm.xml .\queries" - Process all the hbm.xml files in the directory and output the generated queries to the queries directory.
Note that you should include all the files in that directory, since the tool generates a QueryBuilder file that goes along with the generated code. - From Visual Studio:
Go to the propeties of an hbm.xml file, and set the custom tool to: "NHibernateQueryGenerator"
Note, you still need the query builder file, which you can get from the zip or by running the tool from the command line manually (which will create it). - If you are not using the recent vesion of Active Record or NHibernate (and I can assure you that you aren't, unless by recent you mean Saturady 14/10/2006 - about half an hour before this is posted) you can use the versions that comes with the tool. By default it installs to:
C:\Program Files\Rhino\NHibernate Query Generator\ and you can get them from there.
Limitations:
- Requires NHibernate 1.2 (from the trunk, at the moment) and the latest of
- The VB.Net support is mostly untested, and I would really like to hear from a developer working with this and NQG.
Reporting Bugs:
This is a small (the core generator is 600 Lines of code and the supporting query builders are another 400) piece of code, but it has to deal with a lot of conditions, I think that I made a good job in squashing them all, but you might hit a snag or two along the way. Usually in the more compelx/crazy mappings.
Download:
You can get the sources in the new repository, here (html view), or just grab the binaries from the usual place.
Special thanks:
Thanks to William Pierce for sending a patch to handle non qualified names and composite-id properties.
Comments
Comment preview