Skip to content

Instantly share code, notes, and snippets.

@davepermen
Created May 21, 2013 10:23
Show Gist options
  • Save davepermen/5618829 to your computer and use it in GitHub Desktop.
Save davepermen/5618829 to your computer and use it in GitHub Desktop.
//davepermen
interface IFilterer
{
Expression<Func<Message, bool>> Filter(string Value);
}
static class Filterer
{
static public IFilterer Get(FilterParameterOperatorEnum op)
{
var name = System.Enum.GetName(typeof(FilterParameterOperatorEnum), op);
var type = Type.GetType("ConsoleApplication1." + name + "Filterer"); // needs your namespace right there
return Activator.CreateInstance(type) as IFilterer;
}
}
class EqualToFilterer : IFilterer
{
public Expression<Func<Message, bool>> Filter(string Value)
{
return message => !string.IsNullOrEmpty(message.Body) &&
message.Body
.Equals(Value, StringComparison.InvariantCultureIgnoreCase);
}
}
class NotEqualToFilterer : IFilterer
{
public Expression<Func<Message, bool>> Filter(string Value)
{
return message => !string.IsNullOrEmpty(message.Body) &&
!message.Body
.Equals(Value, StringComparison.InvariantCultureIgnoreCase);
}
}
class ContainsFilterer : IFilterer
{
public Expression<Func<Message, bool>> Filter(string Value)
{
return message =>
!string.IsNullOrEmpty(message.Body) &&
message.Body.IndexOf(Value,
StringComparison.InvariantCultureIgnoreCase) >= 0;
}
}
class DoesNotContainFilterer : IFilterer
{
public Expression<Func<Message, bool>> Filter(string Value)
{
return message =>
!string.IsNullOrEmpty(message.Body) &&
message.Body.IndexOf(Value,
StringComparison.InvariantCultureIgnoreCase) == -1;
}
}
//jchannon
class MessageFilterer
{
private FilterParameterOperatorEnum operatorEnum;
private string value;
public Expression<Func<Message, bool>> FilterData()
{
var filterer = Filterer.Get(operatorEnum);
filterer.Filter(value);
return null;
}
}
enum FilterParameterOperatorEnum
{
EqualTo,
NotEqualTo,
Contains,
DoesNotContain
}
class Message
{
public string Body { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment