JRuby on Rails using Windows! Five Reasons why Monkeys > #Fail Project Managers!
Feb 07

filter-256

 

 

First of all, we need to understand the concept of pipelines and filters, Wikipedia says:

Pipeline (computing)

Instruction scheduling on the Intel Pentium 4.

In computing, a pipeline is a set of data processing elements connected in series, so that the output of one element is the input of the next one. The elements of a pipeline are often executed in parallel or in time-sliced fashion; in that case, some amount of buffer storage is often inserted between elements.

 

Filter (higher-order function)

In functional programming, filter is a higher-order function that processes a data structure (typically a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.

 

Basically we can create a pipeline with filters resulting in some final product we want.

Let’s make it clear:

We have a class called PC with the properties LCD (boolean) and WiFi(boolean):

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace PipeLinesAndFilters
  7. {
  8.     public class PC
  9.     {
  10.         public bool LCD { get; set; }
  11.         public bool WiFI { get; set; }
  12.     }
  13. }

We have 3 PCS:

PC LCD WiFi
1 true true
2 true false
3 false false

 

We code this:

Code Snippet
  1. PC pc1 = new PC();
  2. pc1.LCD = true;
  3. pc1.WiFI = true;
  4.  
  5. PC pc2 = new PC();
  6. pc2.LCD = true;
  7. pc2.WiFI = false;
  8.  
  9. PC pc3 = new PC();
  10. pc3.LCD = false;
  11. pc3.WiFI = false;

 

We put this pcs into a list

Code Snippet
  1. List<PC> lstPcs = new List<PC>();
  2.  
  3. PC pc1 = new PC();
  4. pc1.LCD = true;
  5. pc1.WiFI = true;
  6. lstPcs.Add(pc1);
  7.  
  8. PC pc2 = new PC();
  9. pc2.LCD = true;
  10. pc2.WiFI = false;
  11. lstPcs.Add(pc2);
  12.  
  13. PC pc3 = new PC();
  14. pc3.LCD = false;
  15. pc3.WiFI = false;
  16. lstPcs.Add(pc3);

 

So now we start working with pipelines and filters.

We will create extension methods(Here’s my post about extension methods) so we can filter this list, and then we’ll be able to do something like that:

Code Snippet
  1. lstPcs.WithLCD(true);

 

Or

Code Snippet
  1. lstPcs.WithWiFi(false);

 

Now we must create our PCFilters class and implement our extension methods.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace PipeLinesAndFilters
  7. {
  8.     public static class PCFilters
  9.     {
  10.         public static List<PC> WithLCD(this List<PC> lstPCS, bool lcd)
  11.         {
  12.  
  13.         }
  14.  
  15.         public static List<PC> WithWiFi(this List<PC> lstPCS, bool wifi)
  16.         {
  17.  
  18.         }
  19.     }
  20. }

 

Ok, we have our methods, but we need to return something, and that is our primary list List<PC> lstPCS filtered.

Code Snippet
  1. public static class PCFilters
  2. {
  3.     public static List<PC> WithLCD(this List<PC> lstPCS, bool lcd)
  4.     {
  5.         return lstPCS.FindAll(p => p.LCD == lcd);
  6.     }
  7.  
  8.     public static List<PC> WithWiFi(this List<PC> lstPCS, bool wifi)
  9.     {
  10.         return lstPCS.FindAll(p => p.WiFI == wifi);
  11.     }
  12. }

 

Now you can try filter our primary list using this extension methods (our filters) and create pipelines with this filters resulting in anything you want:

Code Snippet
  1. lstPcs.WithLCD(true).WithWiFi(false);
  2. lstPcs.WithWiFi(true).WithLCD(true);

 

Ok! Our Pipelines and filters works like a charm  :P

It may sound stupid when you have a class with 2 properties, but it can make your code really clear when you have a big project with hundreds of classes and properties.

Get your logic classes away from LinqToSql queries is nice :)

PS1 – Let your comments and spread the word, publish my post in your social media networks if you like.

PS2 – Feel free to find me on your social networks using the right bar!

 

Thanks everyone and cya!

  • Share/Bookmark

One Response to “.NET Pipelines and Filters!”

  1. [...] This post was mentioned on Twitter by Felipe P Gentil and Talita Facirolli, Caspar Kleijne. Caspar Kleijne said: RT @stJhimyBlog: .NET Pipelines and Filters! – http://bit.ly/bmJ3Ey #dotnet #csharp #programming #development [...]

Leave a Reply

preload preload preload