2011-04-14

Reusability and discoverability of LINQ Queries

I agree with Ayende point that Repositories and DAL’s are pretty much a thing of the past.

Usually my LINQ queries contains 3 distinct parts:

  • Some filtering
  • Some transformation
  • Some selection

If I search for LINQ reusability I get some hits where all 3 parts are reused together. But in my experience it is extremely rare I need the same 3 parts in more than 1 place.

In my experience it is often the filtering part that is used many places. I did not have a good solution for this, until I read ayendes post.

After some though I came up with the following.

linq1

 

 

 

 

 

 

 

 

 

 

linq2

 

 

 

 

 

 

 

 

 

linq3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Notice how the filtering is reusable, and chainable in LINQ . Also the code reads very well and the where clauses are easily discoverable with intellisence.

This approach is very light weight and flexible, just like code should be.

No comments: