views:

189

answers:

2

I'm guessing most of us have to deal with this at some point so I thought I'd ask the question.

When you have a lot of collections in your BLL and you find that you're writing the same old inline (anonymous) predicates over and over then there's clearly a case for encapsulation there but what's the best way to achieve that?

The project I'm currently working on takes the age old, answer all, static class approach (E.g User class and static UserPredicates class) but that seems somewhat heavy-handed and a little bit of a cop out.

I'm working in C# mostly so keeping in that context would be most helpful but i think this is generic enough a question to warrant hearing about other languages.

Also I expect there will be a difference in how this might be achieved with the advent of LINQ and Lambdas so I'd be interested in hearing how this could be done in both .Net2.0 and 3.0/3.5 styles.

Thanks in advance.

+2  A: 

Specification pattern might be worth checking out.
With some polymorphism & usage of generics it should work.

Arnis L.
you got in first so points and kudos to you.
Stimul8d
I hope it helped.
Arnis L.
+1  A: 

A Predicate is essentially just an implementation of the Specification design pattern. You can read about the Specification pattern in Domain-Driven Design.

Mark Seemann
Thanks for the book link,...yet another added to the wish list!
Stimul8d
That book is highly recommended depite that DDD is damn hard at start. Add to your wishlist this one too: http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202
Arnis L.