QuerySpecificationExtensions Use

Sep 3, 2010 at 2:45 AM
Edited Sep 3, 2010 at 11:04 PM

I'm not quite sure how/when you would use the And/Or in QuerySpecificationExtensions. Could you elaborate with an example? Is there a place in WCHM that uses this?

Sep 3, 2010 at 5:32 AM
No, I'm afraid not - and I don't think WCHM gives a great example of how to use the specifications.

Obviously you're familiar with Stack Overflow, so imagine you were writing specifications to deal with their dataset. Within their domain they have several immediately obvious concepts:
- Active questions
- Hot questions
- Unanswered questions
- Newest questions
- Questions by one of a large number of tags.

Using the specification pattern, you can take a domain concept such as "active questions" and wrap up what that means in a single class. You could decide that "active" meant "someone has replied within the list 6 hours" - which is the rule you'd code as the expression within the specification.

Assuming you did that for all of the above, you'd end up with a pile of specifications named in ways that make obvious sense in the domain:
- ActiveQuestionsSpecification
- HotQuestionsSpecification
.. etc

Now, you come to query the dataset. First a user hits the home page which results in...

var results = this.questionsRepository.FindAll(new ActiveQuestionsSpecification());

Then they hit the Unanswered button and choose the "Newest" tab option...

var results = this.questionsRepository.FindAll(new UnansweredQuestionsSpecification()
                                          .And(new NewestQuestionsSpecification()));

Finally they decide they only want to see C# related questions...

var results = this.questionsRepository.FindAll(new UnansweredQuestionsSpecification()
                                          .And(new NewestQuestionsSpecification())
                                          .And(new QuestionsTaggedWithSpecification("C#")));

So by isolating specific domain concepts in specifications and then using the extensions to chain them together, you can greatly simplify your code. The only thing I'd suggest is that if you do a lot of this stuff you invest in a copy of NHibernate Profiler to make it easy to keep on top of the kind of queries that are getting generated. By and large it's pretty good but occasionally you can do something that results in a hideously ineffecient query and without visibility of what's going on under the covers it can turn into a time toilet.


On 3 September 2010 03:45, ptiseo <notifications@codeplex.com> wrote:

From: ptiseo

I'm nort quite sure how/when you would use the And/Or in QuerySpecificationExtensions. Could you elaborate with an example? Is there a place in WCHM that uses this?

Read the full discussion online.

To add a post to this discussion, reply to this email (whocanhelpme@discussions.codeplex.com)

To start a new discussion for this project, email whocanhelpme@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Sep 4, 2010 at 12:13 AM

Thanks. I have invested in NProfiler already and it has been...interesting. A simple two-join query across an association table turns into wuite a mess. Well, I'll try to optimize later, when it matters.

Sep 7, 2010 at 2:40 PM

good question.  i never used the extensions -- very cool.