Skip to main content

Module value_object

Module value_object 

Source
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§

ValueObjectError
Common validation error for value objects.

Traits§

OptionalValueObject
Extension trait for optional value objects.
ValueObject
DDD Value Object trait.