Expand description
Specification Pattern - Business Rules as Objects
The Specification pattern encapsulates business rules that can be combined and reused. This is a core DDD tactical pattern.
§Example
ⓘ
use backbone_core::specification::{Specification, AndSpecification};
// Define a specification
struct UserIsActive;
impl Specification<User> for UserIsActive {
type Error = String;
fn is_satisfied_by(&self, user: &User) -> Result<bool, Self::Error> {
Ok(user.status == UserStatus::Active)
}
}
// Combine specifications
let spec = UserIsActive.and(UserHasVerifiedEmail);
let is_valid = spec.is_satisfied_by(&user)?;Structs§
- Always
False - Always returns false
- Always
True - Always returns true
- AndSpecification
- AND specification - both left and right must be satisfied
- NotSpecification
- NOT specification - negates the inner specification
- OrSpecification
- OR specification - either left or right must be satisfied
- Predicate
Specification - Specification using a closure
- Specification
Evaluator - Evaluates multiple specifications and collects results
- Specification
Result - Result of evaluating a specification with detailed information
Traits§
- Specification
- Core Specification trait
Functions§
- predicate
- Helper function to create a predicate specification