254
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
Implement the Query Specification pattern
As introduced earlier in the design section, the Query Specification pattern is a Domain-Driven Design
pattern designed as the place where you can put the definition of a query with optional sorting and
paging logic.
The Query Specification pattern defines a query in an object. For example, in order to encapsulate a
paged query that searches for some products you can create a PagedProduct specification that takes
the necessary input parameters (pageNumber, pageSize, filter, etc.). Then, within any Repository
method (usually a List() overload) it would accept an IQuerySpecification and run the expected query
based on that specification.
An example of a generic Specification interface is the following code, which is similar to code used in
the
eShopOnWeb
reference application.
// GENERIC SPECIFICATION INTERFACE
// https://github.com/dotnet-architecture/eShopOnWeb
public
interface
ISpecification
{
Expressionbool
>> Criteria {
get
; }
Listobject
>>> Includes {
get
; }
List<
string
> IncludeStrings {
get
; }
}
Then, the implementation of a generic specification base class is the following.
// GENERIC SPECIFICATION IMPLEMENTATION (BASE CLASS)
// https://github.com/dotnet-architecture/eShopOnWeb
Dostları ilə paylaş: