Search code examples
c#lambdaspecification-pattern

Specification pattern - creating compound specifications using lambdas (C#)


If I have a specification defined as an Expression as below:

public Expression<Func<Foo, bool>> IsSuperhuman = 
  x => x.CanFly && x.HasXRayVision;

And I want to define another specification 'IsSuperheroine' with the logic 'is superhuman and is female', how can I reuse the existing specification within the new one?


Solution

  • Have you checked out predicate builder in LinqKit? It builds up expressions by letting you and and or expressions together.