25 de marzo de 2010

CodeRush plugin to extract NHibernate's hql queries as Named Queries in a separate xml file

I have created a DevExpress CodeRush plugin to extract hql queries as Named Queries to an xml mapping file.

The idea is to write the hql query in code, like this:

var query = session.CreateQuery("from Order o where o.Amount > :amount");
query.SetParameter("amount", 100);
return query.List<Order>();

Then, position the cursor over the hql query, in this case "from Order o where o.Amount > :amount", and select the refactor option "Extract Hql Named Query".

The plugin then tries to locate the mapping file, and adds the query as a Named query.

After the query is added in the xml file, you can give a name to the named query, which will be synchronized in both files (like que CodeRush's rename F2)

Please note that after the query has been extracted, you should manually change the CreateQuery method with GetNamedQuery

Currently the plugin supports only a fixed name for the xml mapping file, and two strategies to locate that file:

* In current project only (default)
* First in current project, then in solution

This can be configured in the plugin options page.

Also, the refactor command can be configured with a key shortcut

Information about the plugin and download link is placed in the CR_ExtractHqlNamedQuery wiki page on the DXCore Community Plugins project.

Special thanks to Rory Becker for all the information he has shared about DXCore plugin development

No hay comentarios:

Publicar un comentario