Skip to content

Initialize the domain

DDD CQRS ES

The domain is initialized by calling the init method.

domain.init()

Domain.init()

When you call init, Protean does the following:

1. Traverse the domain model

By default, Protean traverses the directory structure under the domain file to discover domain elements. You can control this behavior with the traverse flag:

domain.init(traverse=False)

If you choose to not traverse, Protean will not be able to detect domain elements automatically. You are responsible for registering each element with the domain explicitly.

2. Construct the object graph

Protean constructs a graph of all elements registered with a domain and exposes them in a registry.

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

domain = Domain()


@domain.aggregate
class User:
    name: String(max_length=50)


@domain.entity(part_of=User)
class Credentials:
    email: String(max_length=254)
    password_hash: String(max_length=128)


@domain.event(part_of=User)
class Registered:
    id: Identifier()
    email: String()
    name: String()
    password_hash: String()


print(domain.registry.elements)
""" Output:
{
    'aggregates': [<class '__main__.User'>],
    'events': [<class '__main__.Registered'>],
    'entities': [<class '__main__.Credentials'>]
}
"""

3. Initialize dependencies

domain.init() connects to the underlying infra, tests access, and makes the infra available to the rest of the system.

By default, a protean domain is configured to use in-memory replacements for infrastructure, like databases, brokers, and caches. They are useful for testing and prototyping. But for production purposes, you will want to choose a database that actually persists data.

from protean import Domain

domain = Domain()

domain.config["databases"]["default"] = {
    "provider": "sqlite",
    "database_uri": "sqlite:///test.db",
}

domain.init(traverse=False)

In the example above, the domain activates an SQLite database repository and makes it available for domain elements for further use.

Refer to Configuration handling to understand the different ways to configure the domain.

4. Validate Domain Model

In the final part of domain initialization, Protean performs additional setup tasks on domain elements. Protean also runs checks to confirm that the domain model is specified correctly.

Examples of checks include:

  1. Resolving references that were specified as Strings, like:
@domain.entity(part_of="User")
class Account:
    email: String(max_length=254)


@domain.aggregate
class User:
    name: String(max_length=50)
  1. Setting up Aggregate clusters and their shared settings. Protean uses the object graph it constructed earlier to apply the same settings, like the stream category and database provider, to all elements under an aggregate.

  2. Constructing a map of command and event types to reference when processing incoming messages later.

  3. Running checks that confirm the domain structure is valid.