Skip to main content

ValueObject

Trait ValueObject 

Source
pub trait ValueObject:
    Clone
    + Eq
    + Hash
    + Debug
    + Send
    + Sync {
    type Error: Error + Send + Sync;

    // Required method
    fn validate(&self) -> Result<(), Self::Error>;

    // Provided methods
    fn new_validated(self) -> Result<Self, Self::Error>
       where Self: Sized { ... }
    fn is_valid(&self) -> bool { ... }
}
Expand description

DDD Value Object trait.

Value Objects are characterized by:

  • Immutability (once created, they don’t change)
  • Equality based on attributes, not identity
  • Self-validation

§Required Bounds

  • Clone: Value objects should be copyable
  • Eq + Hash: Equality based on attributes
  • Debug: For debugging and logging
  • Send + Sync: Thread safety for async contexts

Required Associated Types§

Source

type Error: Error + Send + Sync

Error type for validation failures.

Required Methods§

Source

fn validate(&self) -> Result<(), Self::Error>

Validate the value object.

Returns Ok(()) if the value object is valid, or an error describing the validation failure.

Provided Methods§

Source

fn new_validated(self) -> Result<Self, Self::Error>
where Self: Sized,

Create a validated instance.

This is a convenience method that validates after construction.

Source

fn is_valid(&self) -> bool

Check if this value object is valid without returning an error.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§