For Linq scenarios, use ILinqSpecification<T>
interface from FluentSpecification.Abstractions
package.
ILinqSpecification<T>
inherit from ISpecification<T>
.
public class ExpiredCreditCardSpecification :
ILinqSpecification<CreditCard>
{
public Expression<Func<CreditCard, bool>> GetExpression()
{
DateTime now = DateTime.Now;
return creditCard => creditCard.ValidityDate < now;
}
Expression ILinqSpecification.GetExpression()
{
return GetExpression();
}
public bool IsSatisfiedBy(CreditCard candidate)
{
return GetExpression().Compile().Invoke(candidate);
}
}
You don't need to use Expression in IsSatisfiedBy
method, but it's highly recommended to provide the same result here and here.