Skip to content

Instantly share code, notes, and snippets.

@SeanKilleen
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanKilleen/eb5c94beaed13055cc8c to your computer and use it in GitHub Desktop.
Save SeanKilleen/eb5c94beaed13055cc8c to your computer and use it in GitHub Desktop.
public interface ISieve<TTypeOfObjectToFilter>
{
ISieve<TTypeOfObjectToFilter, TPropertyType> ForProperty<TPropertyType>(Expression<Func<TTypeOfObjectToFilter, TPropertyType>> propertyExpression);
}
/// <summary>
/// Every Sieve (EqualitySieve, LessThanSieve, GreaterThanSieve) should supply this functionality.
/// </summary>
/// <typeparam name="TTypeOfObjectToFilter"></typeparam>
/// <typeparam name="TPropertyType"></typeparam>
public interface ISieve<TTypeOfObjectToFilter, TPropertyType>
{
InvalidValueBehavior InvalidValueBehavior { get; }
EmptyValuesListBehavior EmptyValuesListBehavior { get; }
IEnumerable<string> DefaultSeparators { get; }
IEnumerable<string> Separators { get; }
PropertyInfo PropertyToFilter { get; }
Expression<Func<TTypeOfObjectToFilter, bool>> ToExpression();
Func<TTypeOfObjectToFilter, bool> ToCompiledExpression();
ICollection<TPropertyType> AcceptableValues { get; }
ISieve<TTypeOfObjectToFilter, TPropertyType> ForProperty(Expression<Func<TTypeOfObjectToFilter, TPropertyType>> propertyExpression);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForValue(string stringValue);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForValue(TPropertyType acceptableValue);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForValues(IEnumerable<string> acceptableValues);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForValues(IEnumerable<TPropertyType> acceptableValues);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForValues(string valuesListToParse);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForAdditionalValue(string additionalValue);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForAdditionalValue(TPropertyType additionalValue);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForAdditionalValues(IEnumerable<string> acceptableValues);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForAdditionalValues(IEnumerable<TPropertyType> listOfValues);
ISieve<TTypeOfObjectToFilter, TPropertyType> ForAdditionalValues(string listOfValues);
ISieve<TTypeOfObjectToFilter, TPropertyType> WithSeparator(string newSeparatorString);
ISieve<TTypeOfObjectToFilter, TPropertyType> WithSeparators(IEnumerable<string> separatorStrings);
ISieve<TTypeOfObjectToFilter, TPropertyType> WithEmptyValuesListBehavior(EmptyValuesListBehavior emptyValuesListBehavior);
ISieve<TTypeOfObjectToFilter, TPropertyType> WithInvalidValueBehavior(InvalidValueBehavior invalidValueBehavior);
}
public interface IFindableSieve<TTypeOfObjectToFilter>
{
ISieve<TTypeOfObjectToFilter> GetSieve();
}
// The end goal is to be able to do something like this.
// This is entirely pseudo-code.
// The general idea -- be able to find all of the appropriate sieves for a given propery or name,
// and then pass in all the values those Sieves should accept.
// Then, the resulting sieves coudl be passed into something like an OR/M as expressions,
// and something like a SQL where statement could be constructed from them.
//obviously this would be designed better and built-in to Sieve .NET itself.
var filterNamesAndValuesFromQueryString = new NameValueCollection
{
{"LastName", "Smith, Johnson, Jones"},
{"FirstName", "Harry, Sally, Frank"}
};
var keys = filterNamesAndValuesFromQueryString.GetAllKeys();
var sieves = new SieveLocator<Person>().GetSievesForProperties(keys); // returns instances of applicable sieves.
foreach(var keyItem in keys)
{
var matchingSieves = sieves.Where(x=>x.PropertyToFilter.Name == keyItem);
var result = new List<ISieve<Person>>();
foreach (var sieveItem in matchingSieves)
{
result.Add(sieve.ForValues(filterNamesAndValuesFromQueryString[keyItem]).WithSeparator(","));
}
}
// at this point, we'd use a predicate builder or something to "And" sieveItem.ToExpression() to the other Sieve items
// and create something that could be parsed by an OR/M.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment