ddd-toolkit-core 0.2.1

Traits and reference implementations for Domain-Driven Design building blocks: entities, value objects, aggregates, repositories, and event dispatch.
Documentation

ddd-toolkit-core

Traits and reference implementations for Domain-Driven Design building blocks in Rust: entities, value objects, aggregates, repositories, and event dispatch. No macro dependency - everything here can be implemented by hand.

If you want #[derive(...)] support for the value-object-shaped traits, add ddd-toolkit-macro alongside this crate, or depend on ddd-toolkit instead, which bundles both behind a single dependency.

What's here

  • domain - Entity, AggregateRoot, ValueObject/Wrapped, EntityId, SecretVo, EnumVo, DomainEvent, ValidationError.
  • port - Load/Save/Delete repository traits, EventDispatcher (with DispatchError<E> reporting undelivered events on partial failure), Clock, IdGenerator, PortError.
  • adapter (behind the chrono/uuid features) - SystemClock, UuidV4Generator, UuidV7Generator.
  • mock - InMemoryStore (a Load/Save/Delete implementation backed by a HashMap), FixedClock, FixedIdGenerator.
  • application - UseCase.

Example

use ddd_toolkit_core::domain::{Entity, EntityId, ValueObject};
use std::fmt::Display;

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct OrderId(String);

impl ValueObject for OrderId {}

impl Display for OrderId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl EntityId for OrderId {}

struct Order {
    id: OrderId,
}

impl Entity for Order {
    type Id = OrderId;

    fn id(&self) -> &Self::Id {
        &self.id
    }
}

fn main() {
    let a = Order { id: OrderId("order-1".to_string()) };
    let b = Order { id: OrderId("order-1".to_string()) };
    assert!(a.is_same_as(&b)); // identity, not structural equality
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.