Skip to main content

Event Handler

Description

Reacts to a specific type of Domain Event, performing side effects or triggering further actions.

Definition Schema

The definition for a event_handler component expects the following structure:

{
"$defs": {
"EventHandlerDependencyInputDTO": {
"description": "Defines a dependency to be injected into the Event Handler's __init__.",
"example": {
"assigned_attribute_name": "_emailer",
"param_name": "email_service",
"param_type_str": "EmailService"
},
"example_with_explicit_import": {
"param_import_from_module": "app_root_name.services.notifications",
"param_import_names": [
"NotificationService"
],
"param_name": "notification_svc",
"param_type_str": "NotificationService"
},
"properties": {
"param_name": {
"description": "Name of the parameter in __init__ (e.g., 'user_repository')",
"title": "Param Name",
"type": "string"
},
"param_type_str": {
"description": "Type hint string for the parameter (e.g., 'UserRepository')",
"title": "Param Type Str",
"type": "string"
},
"assigned_attribute_name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Name of the attribute to assign the dependency to on self (e.g., '_repo'). Defaults to '_' + param_name if None.",
"title": "Assigned Attribute Name"
},
"param_import_from_module": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Optional: Explicit module to import the dependency type from (e.g., 'app_root_name.services.email').",
"title": "Param Import From Module"
},
"param_import_names": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"description": "Optional: Explicit list of names to import from the module (e.g., ['EmailService']). Used if param_import_from_module is set.",
"title": "Param Import Names"
}
},
"required": [
"param_name",
"param_type_str"
],
"title": "EventHandlerDependencyInputDTO",
"type": "object"
}
},
"description": "DTO for defining an Event Handler class.",
"example": {
"dependencies": [],
"event_bus_instance_name": "event_bus",
"event_module": "generated_events",
"event_name": "PostCreatedEvent",
"name": "LogPostCreationHandler"
},
"example_with_deps": {
"dependencies": [
{
"assigned_attribute_name": "_notifier",
"param_name": "notification_service",
"param_type_str": "NotificationService"
}
],
"event_bus_instance_name": "event_bus",
"event_module": "generated_events",
"event_name": "OrderPlacedEvent",
"name": "NotifyAdminOnOrderPlacedHandler"
},
"properties": {
"name": {
"description": "Name of the handler class (e.g., 'LogPostCreationHandler')",
"title": "Name",
"type": "string"
},
"event_name": {
"description": "Name of the event class it handles (e.g., 'PostCreatedEvent')",
"title": "Event Name",
"type": "string"
},
"event_module": {
"description": "Module where the event class is defined (e.g., 'generated_events')",
"title": "Event Module",
"type": "string"
},
"dependencies": {
"description": "List of dependencies for the __init__ method.",
"items": {
"$ref": "#/$defs/EventHandlerDependencyInputDTO"
},
"title": "Dependencies",
"type": "array"
},
"event_bus_instance_name": {
"default": "event_bus",
"description": "Name of the event bus instance variable used for the @register decorator.",
"title": "Event Bus Instance Name",
"type": "string"
}
},
"required": [
"name",
"event_name",
"event_module"
],
"title": "EventHandlerInputDTO",
"type": "object"
}

Naming and Location Conventions

Class: PascalCase, typically ends with 'EventHandler'. File: ${event_handler_name}.py (based on ${event_handler_name}). Location: app/application/${bounded_ctx}/handlers. (Requires context: ['bounded_ctx'])

Example Definition

{
"name": "LogPostCreationHandler",
"event_name": "PostCreatedEvent",
"event_module": "generated_events",
"dependencies": [],
"event_bus_instance_name": "event_bus"
}