Wednesday, July 23, 2008
Parameter Support (Contrived Example)
Continuing the Library example, how would you query the books that will be overdue next week?
Well you could write the following:
How it works
The CheckOverdue Property simply returns a Lambda Expression as it's result.
I've modified the expression parsing routines to support parameters (including multiple) and even nested lambdas!
I can't really see the need for it, but hey... why not! Curry anyone? :)
So how would you call such a monstrosity?
But check out the SQL! Great work Linq to Sql guys!
If you're particularly astute, you'll have noticed that my syntax for declaring a QueryProperty has changed slightly since the last post.
Here's the class declaration of the CheckOverdue Property:
Although this is a method, you can still define it as a property (notice that I return a lambda expression).
So What's Changed?
Well, QueryProperties no longer derive from an Attribute class. Although it worked, I felt dirty about it.
I borrowed from Fredrik's syntax and pass the property type to the Attribute - much cleaner.
Since I'm no longer defining the QueryProperty as static, I had to change the way it is referenced by the class property.
The code above shows my attempt at making this easy, yet efficient - but it's up to you how to implement it. E.g. this would work well too:
If you can think of better ways I'd love to hear it!
Since I posted the parser code in my last post, here it is again - and my has it grown!
As usual, here is the sample code for you to experiment with yourself.
I hope you've enjoyed this series, I certainly have.
If I get the chance I might write a post explaining the expression parsing in detail, or the compiled queries - but till then...
Thursday, July 17, 2008
So what's changed?
Not much. Defining your query properties has changed (and obviously some implementation details). All the linq syntax stuff stays the same.
Here's the same Loan class, rewritten:
And here's our query property definition:
What's the difference?
Well, depending on your programming tastes, it's cleaner. We've reduced some of the tight coupling, and we now have the ability to spread the logic (which is more suitable in certain situations).
And if you're wondering why I seem to have an obsession with static members, it's mainly due to performance - and truth be told, I'm pre-optimizing here. Generally I'm not such a fan... honest!
Speaking of performance, using Attributes has a penalty. According to my tests, it's roughly 30% slower to parse the expression.
This might sound like a lot, but we're talking about 0.37 vs 0.28 milliseconds per query here - and once you consider the database retrieval time... it's really nothing.
I don't even parse the expression tree with linq to objects - so no impact at all!
Bonus code (Expression Parser)
Here's my expression parsing code, I know you want to see it:
Beautiful isn't it!
If you've been following this little series, I'd love to hear what you think of the new syntax.
I'm still hesitant, since this new syntax results in more code - but I think it's a cleaner approach.
As usual, you can find the sample app here - enjoy!
Wednesday, July 16, 2008
See the changes
I took my rewrite as an opportunity to support the full query syntax:
and added easy support for compiled queries (currently only supporting linq to objects and linq to sql):
I've also optimized the expression parsing (speed difference is negligible to normal queries), and I tweaked the QueryProperty construction:
The big change here is that I've added an extra parameter; a reference back to the mapping property.
I'd really like a memberof(Loan.IsOverdue) keyword style syntax in C#, but since there's not, I use this expression to grab the MemberInfo - which is used in the expression parser.
It's a slight DRY drawback, but it's a faster way than using reflection (and avoids an ugly string lookup) - and it's much faster than using attributes.
Get the code
Most of the big changes are all implementation specific, so be sure to check out the sample app.
Please let me know what you think!
BTW: Joseph, I made some architectural changes to the LinqKit.ExpandableQuery stuff. Externally it behaves exactly the same, but it now lets me reuse a lot more code.
It's light on the comments, but I'm hoping my next post will explain myself.
ed: Continue to Part III
Tuesday, July 15, 2008
I'm a huge keyboard shortcut fan (for a windows developer)... for common tasks it's just annoying to have to grab the mouse.
For me, one such task was the rename / resolve support in Visual Studio:
Using the mouse to bring up this dialog has got to be the most frustrating task in Visual Studio.
So much so, I refused to use it; instead opting for the right-click 'resolve' approach, or even manually typing in namespaces.
A few months ago, I stumbled on "Ctrl ." and to my delight ended my coding frustration.
I'm now more productive, less frustrated... do yourself a favour and give "Ctrl ." a go!
Monday, July 7, 2008
With the following class, I can query on IsOverdue using linq... but not using linq to sql (or linq to nhibernate). How frustrating!
This is a fundamental issue in relational to object mapping systems, but we can fix it using some linq trickery.
I think my solution is very cool, but I'm not sure how acceptable it is for a production environment.
Please leave feedback if you like it / makes you want to vomit.
Let's modify the Loan class implementation of IsOverdue:
Here we move the logic for IsOverdue into a separate QueryProperty member that takes care of the nasty details.
Hopefully you'll agree that the code is still fairly readable, and encapsulated with all the logic still bundled inside the Loan class.
By moving this logic into a lambda expression, we can now write queries for both database and object collections! e.g
When coding these query properties it can be very handy to reference other query properties. e.g.
This allows us to create very complicated queries in a very simple manner.
Too good to be true? You're right; there's a devil in the details.
You might have picked it up already - how does linq interpret my query property correctly?
By modifying the expression tree!
My Repository class implements the Where() and translates the expression; replacing any QueryProperty references with a call to the actual lambda expression.
The really ugly part is that it's a string based lookup ie. "<name>Property". Though I also check the type to be extra safe.
It's really not a bad trade off, considering it's very similar to how WPF DependencyProperty's work.
This is a really easy and seamless way to implement custom queryable properties in your domain objects.
I've only touched on a fraction of the possibilities with this technique, but it shows the power and flexibility of linq.
Make sure you check out the sample application for the full code.
ed: Continue to Part II