Skip to content

Instantly share code, notes, and snippets.

@jakkaj
Created November 25, 2025 00:29
Show Gist options
  • Select an option

  • Save jakkaj/1e5b265a4385d68e620216b0bcf3ae41 to your computer and use it in GitHub Desktop.

Select an option

Save jakkaj/1e5b265a4385d68e620216b0bcf3ae41 to your computer and use it in GitHub Desktop.
[md-export] constitution-code-2
from dataclasses import dataclass
from datetime import datetime
from abc import ABC, abstractmethod
# Domain command + IDs used at the boundary of the application layer
@dataclass
class BookingId:
value: str
@dataclass
class CreateBookingCommand:
user_id: str
departure: datetime
origin: str
destination: str
# Application service interface
class BookingApplicationService(ABC):
@abstractmethod
def create_booking(self, command: CreateBookingCommand) -> "Booking":
pass
@abstractmethod
def get_booking(self, booking_id: BookingId) -> "Booking | None":
pass
class BookingApplicationServiceImpl(BookingApplicationService):
def __init__(
self,
bookings: "BookingRepository",
payments: "PaymentAdapter",
):
self._bookings = bookings
self._payments = payments
def create_booking(self, command: CreateBookingCommand) -> "Booking":
# Validation & business rules
# (no HTTP, no SDK types)
self._validate_command(command)
provisional = Booking.provisional(
user_id=command.user_id,
departure=command.departure,
origin=command.origin,
destination=command.destination,
)
# Orchestration via adapters/repositories
payment_result = self._payments.charge(
PaymentRequest.for_booking(provisional),
)
confirmed = provisional.confirm(payment_result)
self._bookings.save(confirmed)
return confirmed
def get_booking(self, booking_id: BookingId) -> "Booking | None":
return self._bookings.find_by_id(booking_id)
def _validate_command(self, command: CreateBookingCommand) -> None:
# Pure validation; raise domain-level errors only
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment