Complexity *= 10, Beauty *= 100
Trying to provide even more capabilities to the NHibernate Query Generator is turning tricky. The issue is that I would like to make stuff as natrual as possible. Due to this, I decided to take a break from my usual habit (sitting in from of the IDE and waiting for a nervous tick to cause characters to appear on the screen), and actually think about what I want to do. Then I went crazy with a mess of overloaded operators and generic query objects.
The result, however, is this:
Post.FindAll(
Where.Post.Blog.Author == ayende &&
(Where.Post.Title == "Overloading" || Where.Post.Title == "Operator")
);
What is new about this? Well, the above statement compiles, and when executed, it creates the following query (variables names and formatting applied, nothing else changed):
SELECT
post.Id AS PostId, post.Title AS PostTitle,
post.Contnet AS PostContent, post.Blog AS PostBlog,
blog.Id AS BlogId, blog.Name AS BlogName,
blog.Author AS BlogAuthor, users.Id AS UsersId,
users.Name AS UsersName, users.Email AS UsersEmail
FROM
Posts post
INNER JOIN Blogs blog
ON post.Blog = blog.Id
LEFT OUTER JOIN Users users
ON blog.Author = users.Id
WHERE
blog.Author = @p0 AND
(
( post.Title = @p1 ) OR
( post.Title = @p2 )
) ;
-- @p0 = '1', @p1 = 'Overloading', @p2 = 'Operator'
Now that is a totally different ball game. Take a look at the OO query and the generated SQL. That it three tables join as simple as anything you can think of.
And you know what? The following query doesn't compiles (type safety, yeah!):
Post.FindAll(
Where.Post.Blog.Author == ayende && Where.Blog.Name == "Ayende"
);
The caveat here is that it is a fairly new (and novel direction) so I don't have the generator up to generate this yet (but I made sure that this will be easy to do).
I was really tempted to keep the code hidden and let you try guess how I did it, but I think I will get enough satisfaction from knowing what this is going to make someone's head spin.
Next on this line, I need to get the code gen working once again, better than ever, and then modify Active Record and Rhino Commons to use this, such fun!
Also, codus to Dan, who discovered how many (direct) method calls there are in this riddle (16).
Comments
Comment preview