Skip to content

Object Model

Applies to: DDD · CQRS · Event Sourcing

Domain elements in Protean have a common structure and share a few behavioral traits.

Meta Options

Protean elements have a meta_ attribute that holds the configuration options specified for the element.

Options are passed as parameters to the element decorator:

from protean import Domain
from protean.fields import Integer, String

domain = Domain(name="Accounts")


@domain.aggregate(stream_category="account")
class User:
    first_name: String(max_length=50)
    last_name: String(max_length=50)
    age: Integer()
In [1]: User.meta_
Out[1]:
{'stream_category': 'accounts::account',
 'abstract': False,
 'aggregate_cluster': User,
 'auto_add_id_field': True,
 'fact_events': False,
 'indexes': (),
 'is_event_sourced': False,
 'database_model': None,
 'provider': 'default',
 'schema_name': 'user',
 'limit': 100,
 'suppress_checks': (),
 'reserved': (),
 'deprecated': None}

abstract

abstract is a common meta attribute available on all elements. An element that is marked abstract cannot be instantiated.

Note

Field orders are preserved in container elements.

Reflection

Protean provides reflection methods to explore container elements. Each of the below methods accept a element or an instance of one.

has_fields

Returns True if the element encloses fields.

fields

Return a dictionary of the fields in the element, keyed by field name, both explicitly defined and internally added.

Raises IncorrectUsageError if called on non-container elements like Application Services or Command Handlers.

declared_fields

Return a dictionary of the declared fields, keyed by field name. This includes the identity field Protean adds automatically, but leaves out the internal _version and _metadata fields.

data_fields

Return a dictionary of the data fields in this element, keyed by field name. Does not include metadata.

Raises IncorrectUsageError if called on non-container elements like Application Services or Command Handlers.

has_association_fields

Returns True if element contains associations.

association_fields

Return a dictionary of the association fields, keyed by field name.

Raises IncorrectUsageError if called on non-container elements.

id_field

Return the identity field of this element, or None if there is no identity field.

has_id_field

Returns True if the element has an identity field.

attributes

Internal. Returns a dictionary of fields that generate a representation of data for external use.

Attributes include simple field representations of complex fields like value objects and associations.

Raises IncorrectUsageError if called on non-container elements

unique_fields

Return a dictionary of the fields marked as unique, keyed by attribute name. These are the same keys attributes returns, so a field that sets referenced_as appears under that name. The identity field is always unique.

Raises IncorrectUsageError if called on non-container elements.