Posts

Showing posts with the label C#

A Concrete Filter for Simple Comparisons

Image
With the abstract Filter class complete, we can focus on implementing the actual comparator logic. We can start with the simplest case: LHS Compator RHS For this, we must first declare an enum to record the type of comparison being made: public enum ComparisonOperator { Equals, NotEquals, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual } We can also create an enum to record a function being applied to the column name.  In this case, we're supporting only three.  However, because not all comparison filters will have a function applied, we've also added a None  filter. public enum ComparisonFunction { Length, ToUpper, ToLower, None } Finally, we can create our concrete ComparisonFilter.  Because the abstract Filter class takes care of all of the chaining and group organization, our ComparisonFilter is far smaller and more focused.  We need only to add two properties: one to record the compariso...

Building a Filter class for WHERE clauses

Image
Previously, we built a data structure capable of organizing filters into groups .   With that framework in place, we must now devise some construct to represent filters.  To do that, let's first identify some basic requirements for a filter: Chainable using logical operators AND/OR and optionally NONE for the first filter in a chain A name on which the filter acts (such as a column name) A value to be compared against A comparator (such as >, =, <, etc) The ability to inverse the filter (NOT) This gives us a good start to a basic filter.  However, we previously identified the need to create subgroups -- is now the right time to implement it?  To answer that, let's take a look at the below example: (A OR (B AND C AND D)) Here we have an inner group  (B AND C AND D).  If we were to name this group X , this could be reduced to: (A OR (X)) With the inner group simplified as X , it's easier to abstract it away and focus on the final requirement: a fil...

Building WHERE Clauses for Query Strings

So I had need to create a way of building queries programmatically -- in this case, specifically for OData.  This proved to be somewhat tricky as the ways in which filters could be applied can get pretty complex. Take for example the following: A > B OR A < C Chaining filters together like this (direct one-to-one) is extremely trivial!  Now let's look at a different example: (A > B) OR (A <  C AND A = D) It's immediately clear that there are two groups here.  In the first group is a fairly simple clause, A > B .  In the second group are two filters ANDed together: A < C and A = D .  Abstractly, we can see that there can be one to many groups chained together with some logical operator which themselves contain one to many filters.  Not too bad!  Now let's look at a final example: (A > B) OR (A < C AND A = D AND (D < E OR F <=  G)) By introducing an inner group , we can show above the simplest case expressing the l...