-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an example showing reuse of predicates
- Loading branch information
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
sample/Ardalis.Sample.Domain/Specs/AdultCustomersByNameSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |