Skip to main content

ドメイン駆動開発のコアモジュール

Project description

dddlib

dddlib is a Python library that provides foundational components for implementing Domain-Driven Design.

This library does not provide application-specific business logic itself. Instead, it aims to provide common concepts that repeatedly appear in DDD design in a type-safe and easy-to-use form.

Positioning

dddlib standardizes the following elements commonly required in the domain layer.

  • Value objects
  • Entities
  • Aggregate roots
  • Domain events
  • Identifiers
  • Domain errors
  • Messages

When these are implemented separately for each application, similar constraints, validations, equality comparisons, and reconstruction logic can easily become scattered. dddlib serves as a foundation for reducing that repetition and making it easier to focus on designing the domain model itself.

Design Policy

dddlib is designed based on the following policies.

  • Make it easy to handle value storage and immutability based on dataclass
  • Provide an implementation that works well with type checking by mypy
  • Keep the code explicit and readable in line with strict ruff rules
  • Avoid concrete business processing and limit the scope to common domain foundations
  • Separate it from dddlib_cli to distinguish library usage from CLI usage

For this reason, dddlib is intended to be used not as a framework, but as a thin foundational layer for domain models.

Provided Features

Value Objects

A value object is an object whose identity is determined by its value rather than by an ID.

In dddlib, the following operations, which are often needed when creating value objects, are consolidated.

  • Validation at creation time
  • Conversion to dictionaries
  • Conversion to display strings
  • Storage as immutable values

By using FrozenBase, behavior equivalent to dataclass(frozen=True) can be handled naturally.

Entities

An entity is an object that has identity through an identifier.

The entity foundation in dddlib supports the following operations.

  • Creating objects with identifiers
  • Internal creation for reconstruction
  • Comparison based on identity
  • Retrieving identifiers

Unlike value objects, entities have a lifecycle. Therefore, it is important to clearly separate identity determination.

Aggregate Roots

An aggregate root is the entry point for protecting domain consistency.

The aggregate root in dddlib inherits the characteristics of an entity while allowing domain events to be accumulated and retrieved.

  • Adding events
  • Getting and extracting events
  • Adding multiple events together

This makes it easier to safely handle events that occur as a result of domain operations.

Domain Events

A domain event represents an important event that occurred within the domain.

In dddlib, emphasis is placed on handling events not as simple dictionaries or strings, but as typed objects. By representing event contents as explicit classes, it becomes easier to connect them to event-driven processing, auditing, and notification processing.

Identifiers

An identifier is a value used to uniquely handle an entity or aggregate.

dddlib provides bases mainly for UUID-based identifiers, consolidating value validation and conversion.

This makes it easier to handle IDs as meaningful types instead of scattering raw str or int values throughout the code.

Errors and Messages

dddlib also standardizes errors and messages handled in the domain layer.

  • DDDError is the foundation for domain errors that have a code and message
  • Message handles message body generation and formatting
  • DDDMessage represents predefined messages with codes

Using this mechanism makes it easier to handle errors consistently across the entire application, rather than confining them to simple exception messages.

Typical Usage Image

dddlib is intended to be used, for example, in the following kind of structure.

  • Define domain IDs by deriving them from UUIDIdentifier
  • Give validation logic to value objects
  • Determine entity identity based on IDs
  • Publish domain events from aggregate roots
  • Explicitly represent business failures with domain errors

This design makes it easier to build models using domain language without pushing too much responsibility into the application layer or infrastructure layer.

Relationship with the CLI

The main body of dddlib is a library.

If you want to support project work using a CLI, refer to the separate package dddlib_cli.

Adoption Approach

When adopting dddlib, it is useful to think through the following order.

  1. First, decide how to represent domain identifiers
  2. Next, extract concepts that should be closed as value objects
  3. Then, define entities and aggregate roots
  4. Finally, align the representation of events and errors

dddlib should be used as a tool for aligning implementation, rather than as something that disrupts this flow.

Notes

  • dddlib is not a completed business application, but a common foundation
  • The domain cannot be established simply by using it as-is, so each project needs to define concrete models
  • CLI details are separated into the dddlib_cli README

License

For this project, refer to LICENSE.md directly under the repository root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dddlib-0.1.0a2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dddlib-0.1.0a2-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file dddlib-0.1.0a2.tar.gz.

File metadata

  • Download URL: dddlib-0.1.0a2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dddlib-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 62f455d059a06311260f1c365f98adee5be33c7057747ca187722a70a567f19a
MD5 c54c089f4cef431fb402680541ce6378
BLAKE2b-256 aff18313d23100ceb376e19d2cc7104b0174bf84938236910e42e82e616686ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for dddlib-0.1.0a2.tar.gz:

Publisher: cd.yml on pochinoritaro/dddlib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dddlib-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: dddlib-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dddlib-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 60bf63ae711d6254cf45ececeade00980c5bafdc903e417fe7cfe1450c7e03e1
MD5 f8bcb02c3a35a3b76d99539d92734259
BLAKE2b-256 8325a23a277153e81d32509ee5aaca306b1850d494023cf3d01ca3a58ee144fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for dddlib-0.1.0a2-py3-none-any.whl:

Publisher: cd.yml on pochinoritaro/dddlib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page