Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Patterns/Practices for encapsulating predicates

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.

like image 994
EightyOne Unite Avatar asked Nov 01 '25 01:11

EightyOne Unite


1 Answers

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

like image 148
Arnis Lapsa Avatar answered Nov 02 '25 16:11

Arnis Lapsa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!