Extending NHibernate Query Generator Code
So it has been out for 24 hours, and I got a couple of bug reports (fixed) and some interesting feedback. Ken is asking how you can extend the generated code and use SQL Functions from it. I had to fix a couple of issues which had to do with the amount of code I generated, right now I reduced the amount of code generated by 40% (with no affect of functionality, of course).
After those changes (which also include making everything partial, we can start extending the Where class, this turned out to be a fairly easy, check this out:
namespace Query
{
public partial class Where
{
public partial class Root_Query_User
{
public virtual QueryBuilder<User> IsInGroup(string groupName)
{
QueryBuilder<User> queryBuilder = new QueryBuilder<User>("this", null);
AbstractCriterion sql = Expression.Sql(
new SqlString("? in (select 'Administrators')"),
groupName, NHibernateUtil.String);
queryBuilder.AddCriterion(sql);
return queryBuilder;
}
}
}
}
This is a silly little expression, but it shows that you still have the power to extend the code to allows whatever you want, and still get very good OO syntax. We can now use this like this:
User.FindOne(Where.User.IsInGroup("Administrators"));
One thing to note, I explicitly extend Root_Query_User, this is because if I want to extend Query_User<T1>, I would need to make the criterion join-safe (in this case it is, but it is better to be safe) which can be quite hard.
Update: After applying Ken's suggestion (in the comments), it shrank to:
public partial class Where
{
public partial class Root_Query_User
{
public virtual QueryBuilder<User> IsInGroup(string groupName)
{
AbstractCriterion criterion = Expression.Sql("? in (select 'Administrators')",
groupName, NHibernateUtil.String);
return FromCriterion(criterion, "this", null);
}
}
}
Much cleaner, thanks Ken.
Comments
Comment preview