Specifications
Specification Pattern is used to define named, reusable, combinable and testable filters for entities and other business objects. More details available here. Shesha provides some automation of specifications usage
Specification Manager
Specification manager allows to manipulate specifications and apply them automatically to any Repository instantiated by the IOC.
Example of usage:
On the example above we activate specifications manually and the GetAll() method automatically appends linq expression to the IQueryable. SpecificationManager is thread-safe and can be used in async methods. When multiple specifications are applied to the same entity type the SpecificationManager combines them with logical And.
Specification base class
Shesha provides base class for specifications - ShaSpecification<T>
. The key difference between Shesha and ABP implementation in integration with the SpecificationManager. ShaSpecification<T>
allows to query additional data from any repository and use it in the generated expression. Important note: all specifications are disabled inside the specification itself. It's required to prevent infinite loops.
Example of specification class:
Global specifications
Global specifications allow to filter data irrespective of the execution context. It may be useful for permissions checks. To make specification global you just need to decorate it with the GlobalSpecificationAttribute.
NOTE: we should be very careful with the global specifications as they affect entire application
Context specifications
The affect of the specifications activated using the SpecificationManager is limited by the execution context. SpecificationManager allows to activate context specifications by two ways:
- Manually using
Use<>()
andUse()
methods (see examples above) - Automatically on the action level (IH: Missing Image was here)
Disable specifications
In some cases it may be useful to disable all specifications. The SpecificationManager allows to do it using DisableSpecifications() method, see example below.
On the action level you can disable all specifications using DisableSpecificationsAttribute:
Front-end support
The Query Builder component allows to use a specifications defined on the back-end as a filter parameter. Specifications are listed in the property list, see the image. Query Builder supports two operations for specifications:
Is satisfied
- specification is integrated into the final query as is, i.e. expression returned fromToExpression
evaluates to true.Is satisfied when
- specification included into the final query only when some client-side logic (pre-condition) evaluates to true.