Expand description
DDD Value Object Pattern
Provides traits for implementing Value Objects in Domain-Driven Design. Value Objects are immutable objects that are defined by their attributes rather than by identity.
§Example
ⓘ
use backbone_core::ValueObject;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct EmailAddress(String);
impl ValueObject for EmailAddress {
type Error = EmailError;
fn validate(&self) -> Result<(), Self::Error> {
if self.0.contains('@') && self.0.len() > 3 {
Ok(())
} else {
Err(EmailError::InvalidFormat)
}
}
}
impl EmailAddress {
pub fn new(email: impl Into<String>) -> Result<Self, EmailError> {
Self(email.into()).new_validated()
}
}Enums§
- Value
Object Error - Common validation error for value objects.
Traits§
- Optional
Value Object - Extension trait for optional value objects.
- Value
Object - DDD Value Object trait.