Skip to content

Commit

Permalink
Adding an example showing reuse of predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Dec 11, 2023
1 parent de3c012 commit b11148c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sample/Ardalis.Sample.Domain/Specs/AdultCustomersByNameSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Ardalis.Specification;

namespace Ardalis.Sample.Domain.Specs;

public class AdultCustomersByNameSpec : Specification<Customer>
{
public AdultCustomersByNameSpec(string nameSubstring)
{
Query.Where(c => CustomerPredicates.IsAdult(c) &&
CustomerPredicates.NameIncludes(c, nameSubstring));
}
}
9 changes: 9 additions & 0 deletions sample/Ardalis.Sample.Domain/Specs/CustomerPredicates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ardalis.Sample.Domain.Specs;

public static class CustomerPredicates
{
public static Func<Customer, bool> IsAdult => customer => customer.Age >= 18;
public static Func<Customer, int, bool> IsAtLeastYearsOld => (customer, years) => customer.Age >= years;
public static Func<Customer, string, bool> NameIncludes =>
(customer, nameString) => customer.Name.Contains(nameString, StringComparison.CurrentCultureIgnoreCase);
}

0 comments on commit b11148c

Please sign in to comment.