public
Last active

Specification Pattern

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
using System;
 
namespace ConsoleApplication3
{
internal class Program
{
private static void Main()
{
var chris = new Person {name = "Chris", age = 36};
 
//Traditional syntax
var startsWithC = Specification.Create<Person>(p => p.name.StartsWith("C"));
var lessThan40 = Specification.Create<Person>(p => p.age < 40);
 
var specification = startsWithC.And(lessThan40);
 
Console.Out.WriteLine(specification.IsSatisfiedBy(chris));
 
//Support lambdas for boolean aggregation.
specification = startsWithC.And(p => p.age < 40);
 
Console.Out.WriteLine(specification.IsSatisfiedBy(chris));
}
}
 
internal class Person
{
public int age;
public string name;
}
 
public static class Specification
{
public static IExpressionSpecification<T> Create<T>(Func<T, bool> func)
{
return new DynamicSpecification<T>(func);
}
}
 
internal class DynamicSpecification<T> : Specification<T>
{
private readonly Func<T, bool> func;
 
public DynamicSpecification(Func<T, bool> func)
{
this.func = func;
}
 
public override bool IsSatisfiedBy(T instance)
{
return func(instance);
}
}
 
public interface ISpecification<T>
{
bool IsSatisfiedBy(T instance);
ISpecification<T> And(ISpecification<T> specification);
ISpecification<T> Or(ISpecification<T> specification);
ISpecification<T> Not(ISpecification<T> specification);
}
 
public interface IExpressionSpecification<T> : ISpecification<T>
{
IExpressionSpecification<T> And(Func<T, bool> expression);
IExpressionSpecification<T> Or(Func<T, bool> expression);
IExpressionSpecification<T> Not(Func<T, bool> expression);
}
 
public abstract class Specification<T> : IExpressionSpecification<T>
{
#region IExpressionSpecification<T> Members
 
public abstract bool IsSatisfiedBy(T instance);
 
public ISpecification<T> And(ISpecification<T> specification)
{
return new AndSpecification<T>(this, specification);
}
 
public ISpecification<T> Or(ISpecification<T> specification)
{
return new OrSpecification<T>(this, specification);
}
 
public ISpecification<T> Not(ISpecification<T> specification)
{
return new NotSpecification<T>(this, specification);
}
 
public IExpressionSpecification<T> And(Func<T, bool> expression)
{
return new AndSpecification<T>(this, new DynamicSpecification<T>(expression));
}
 
public IExpressionSpecification<T> Or(Func<T, bool> expression)
{
return new OrSpecification<T>(this, new DynamicSpecification<T>(expression));
}
 
public IExpressionSpecification<T> Not(Func<T, bool> expression)
{
return new NotSpecification<T>(this, new DynamicSpecification<T>(expression));
}
 
#endregion
}
 
internal abstract class CompositeSpecification<T> : Specification<T>
{
protected CompositeSpecification(ISpecification<T> left, ISpecification<T> right)
{
Left = left;
Right = right;
}
 
public ISpecification<T> Left { get; private set; }
 
public ISpecification<T> Right { get; private set; }
}
 
internal class AndSpecification<T> : CompositeSpecification<T>
{
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
: base(left, right)
{
}
 
public override bool IsSatisfiedBy(T instance)
{
return Left.IsSatisfiedBy(instance) && Right.IsSatisfiedBy(instance);
}
}
 
internal class OrSpecification<T> : CompositeSpecification<T>
{
public OrSpecification(ISpecification<T> left, ISpecification<T> right)
: base(left, right)
{
}
 
public override bool IsSatisfiedBy(T instance)
{
return Left.IsSatisfiedBy(instance) || Right.IsSatisfiedBy(instance);
}
}
 
internal class NotSpecification<T> : CompositeSpecification<T>
{
public NotSpecification(ISpecification<T> left, ISpecification<T> right)
: base(left, right)
{
}
 
public override bool IsSatisfiedBy(T instance)
{
return !Left.IsSatisfiedBy(instance) && !Right.IsSatisfiedBy(instance);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.