Log in | Jump |

jhollingworth

a blog about my life (twitter.com/jhollingworth)
This thing was constructed on March 28, 2008, and it was categorized as C#, NHibernate, Uncategorized, castleproject, visual studio, visual studio 2008.
You can follow comments through the RSS 2.0 feed. You can leave a comment, or trackback.

One thing I love about SubSonic, in fact the reason i switched, was how awsome the querying is. I don’t know why but something like new UserCollection().Where(”UserID”, id).Where(”Password”, password) just does it for me. Going from that back to either HQL or ICriterion was a rather painful experience. A very clever guy called Oren, has come up with a solution to this called NHibernate Query Generator (NQG). You can do some rather nifty things like FindOne(WHERE.User.Name == “James” && WHERE.User.Password == “NotGoingToTellYou”). Many (including Oren) belive that you shouldnt have strings in your code and thus the code to create all these cool queries is autogenerated.

The problem I found was I had to constantly change back & forth between the command line & VS to update the code. To solve this i borrowed a trick from SubSonic. I have created a little button in VS (see below) which will run the query complier for you.

Nhibernate Button

To do this, firstly download the latest version of NQG, then copy the exe file NHQG.exe to an safe location e.g. “c:\Program Files\NHibernate\”. Next you need to open up visual studio, and click Tools -> External Tools and then click add. Give whatever name you want e.g. NHibernate, and then for the command the location of NHQG.exe, in my case “c:\Program Files\NHibernate\NHQG.exe”. In the arguments field, you have a few choices:

  • /Lang: language used, either cs | vb
  • /InputFilePattern: location of either Nhibernate mapping file or the dll (if your using Castle Projects ActiveRecord)
  • /OutputDirectory: Directory you want the files to go to e.g. ./Models/Queries
  • /BaseNamespace: Base namspace for queries, e.g. Priority.Queries

so a complete example would look like “/Lang:cs /InputFilePattern:bin/Priority.dll /OutputDirectory:Models/Queries /BaseNamespace:Priority.Queries”

For Inital directory you just add $(ProjectDir) which sets the starting location to be the root of the project directory. Finally, make sure that it is the first in the menu (it will become clear why in a second).

externaltools2.png

Once you click ok, you will now be able to run the application by clicking on Tools -> NHibernate. To create a button for this, you need to click Tools -> Customize -> Toolbars & then click new, a new menu should appear. Next you need to switch back to Commands, Select Tools and the scroll down until you see all the External Command x’s. Drag External Command 1 onto your new toolbar and then click close. It should change the text from External Command 1 to NHibernate (or whatever name you gave it) and your done!

Hope this helps someone!

Right now that we have

This thing was constructed by .


You can follow comments through the RSS 2.0 feed. You can leave a comment, or trackback.

This thing has 3 Comments

  1. Rob Conery
    Posted March 29, 2008 at 2:07 am | Permalink

    Good stuff - one note though, we do (and have, since 1.0) create structs for you to access your columns:

    Product.Columns.ProductName

    for example is a Struct that returns a string. I know this probably isn’t the reason you needed to move - I just wanted to throw it out there ;).

  2. jhollingworth
    Posted March 29, 2008 at 9:24 am | Permalink

    @Rob I stand corrected! Love the blog btw ;)

  3. Knave
    Posted April 1, 2009 at 3:04 am | Permalink

    One major sore point for me is that it is unable to match clauses across multiple projects.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*