Coverage for src / lexigram / contracts / domain / services.py: 100%
14 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
1"""Domain service and policy contracts."""
3from __future__ import annotations
5from typing import TYPE_CHECKING, Any, Generic, Protocol, TypeVar, runtime_checkable
7if TYPE_CHECKING:
8 from lexigram.contracts.core.result import Result
10T = TypeVar("T")
11T_co = TypeVar("T_co", covariant=True)
14@runtime_checkable
15class DomainServiceProtocol(Protocol):
16 """Protocol for domain services.
18 Domain services encapsulate business operations that don't naturally
19 belong to a single entity or value object. They should be stateless.
20 """
23class PolicyViolationProtocol(Protocol):
24 """Describes why a policy was violated."""
26 code: str
27 message: str
28 details: dict[str, Any]
31@runtime_checkable
32class PolicyProtocol(Protocol, Generic[T]):
33 """Protocol for domain policies.
35 A policy evaluates a domain object or operation and returns
36 a Result indicating whether the policy is satisfied.
37 """
39 async def evaluate(self, subject: T) -> Result[T, PolicyViolationProtocol]:
40 """Evaluate the policy against the subject."""
41 ...
44__all__ = [
45 "DomainServiceProtocol",
46 "PolicyProtocol",
47 "PolicyViolationProtocol",
48 "T",
49 "T_co",
50]