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 copyableEq + Hash: Equality based on attributesDebug: For debugging and loggingSend + Sync: Thread safety for async contexts
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn new_validated(self) -> Result<Self, Self::Error>where
Self: Sized,
fn new_validated(self) -> Result<Self, Self::Error>where
Self: Sized,
Create a validated instance.
This is a convenience method that validates after construction.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".