Specification pattern: Difference between revisions

Content deleted Content added
No edit summary
Moved code to C# 5 to be more concise, actually there is no simplification - just making things generic, no extensions either
Line 134:
</source>
 
=== [[C Sharp (programming language)|C# 35.0]], simplified with generics and extension methods ===
 
<source lang="csharp">
public interface ISpecification<T>
{
bool IsSatisfiedBy(T entitycandidate);
ISpecification<T> And(ISpecification<T> other);
ISpecification<T> AndNot(ISpecification<T> other);
Line 150:
{
public abstract Expression<Func<T, bool>> AsExpression();
public override bool IsSatisfiedBy(T candidate) => AsExpression().Compile()(candidate);
 
public override bool IsSatisfiedBy(T entity)
{
Func<T, bool> predicate = AsExpression().Compile();
return predicate(entity);
}
}
 
public abstract class CompositeSpecification<T> : ISpecification<T>
{
public abstract bool IsSatisfiedBy(T entitycandidate);
public ISpecification<T> OrAnd(ISpecification<T> other) => new AndSpecification<T>(this, other);
 
public ISpecification<T> AndAndNot(ISpecification<T> other) => new AndNotSpecification<T>(this, other);
public ISpecification<T> OrNotOr(ISpecification<T> other) => new OrSpecification<T>(this, other);
{
public ISpecification<T> OrNot(ISpecification<T> other) return=> new AndSpecificationOrNotSpecification<T>(this, other);
public ISpecification<T> Not() return=> new NotSpecification<T>(this);
}
 
public ISpecification<T> AndNot(ISpecification<T> other)
{
return new AndNotSpecification<T>(this, other);
}
 
public ISpecification<T> Or(ISpecification<T> other)
{
return new OrSpecification<T>(this, other);
}
 
public ISpecification<T> OrNot(ISpecification<T> other)
{
return new OrNotSpecification<T>(this, other);
}
 
public ISpecification<T> Not()
{
return new NotSpecification<T>(this);
}
 
}
 
public class AndSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> left;
private readonly ISpecification<T> right;
 
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
Line 200 ⟶ 174:
}
 
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate);
{
return left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate);
}
}
 
public class AndNotSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> left;
private readonly ISpecification<T> right;
 
public AndNotSpecification(ISpecification<T> left, ISpecification<T> right)
Line 217 ⟶ 188:
}
 
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate) != true;
{
return left.IsSatisfiedBy(candidate) && right.IsSatisfiedBy(candidate) != true;
}
}
 
public class OrSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> left;
private readonly ISpecification<T> right;
 
public OrSpecification(ISpecification<T> left, ISpecification<T> right)
Line 234 ⟶ 202:
}
 
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate);
{
return left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate);
}
}
public class OrNotSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> left;
private readonly ISpecification<T> right;
 
public OrNotSpecification(ISpecification<T> left, ISpecification<T> right)
Line 250 ⟶ 215:
}
 
public override bool IsSatisfiedBy(T candidate) => left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate) != true;
{
return left.IsSatisfiedBy(candidate) || right.IsSatisfiedBy(candidate) != true;
}
}
 
public class NotSpecification<T> : CompositeSpecification<T>
{
private readonly ISpecification<T> other;
public ISpecification<T> AndNotNotSpecification(ISpecification<T> other) => this.other = other;
 
public NotSpecificationoverride bool IsSatisfiedBy(ISpecification<T candidate) => !other.IsSatisfiedBy(candidate);
{
this.other = other;
}
 
public override bool IsSatisfiedBy(T candidate)
{
return !other.IsSatisfiedBy(candidate);
}
}
</source>