/design-patterns:service-layerfrom design-patterns
Use when business logic leaks into controllers or handlers, services contain both orchestration and domain decisions, the boundary between use-case coordination and domain rules is unclear, or domain objects have no behaviour. Apply before designing or reviewing any service class. Covers Application Service, Domain Service, and Service Layer — with explicit rules for what belongs where.
Ships in design-patterns. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Sources: Martin Fowler — PoEAA (2002); Eric Evans — Domain-Driven Design (2003)
if order.total > 1000: apply_discount()) live inside service classes| Question | Pattern |
|---|---|
| Coordinating use cases — which repository, which event, which transaction? | Application Service |
| Logic is pure domain but doesn't fit a single entity or value object? | Domain Service |
| Defining the overall API boundary of the application? | Service Layer |
Defines the application's use cases. Orchestrates domain objects, repositories, and infrastructure. Contains no domain logic — if you find an if that a domain expert would recognise as a business rule, it does not belong here.
Pain it removes: business logic scattered in controllers and handlers; no clear place for use-case orchestration; transaction boundaries undefined.
When to use: coordinating multiple domain objects or aggregates; calling external services; managing transactions; publishing domain events.
class OrderApplicationService:
def __init__(
self,
orders: OrderRepository,
payments: PaymentGateway,
events: EventBus
):
self._orders = orders
self._payments = payments
self._events = events
def submit_order(self, command: SubmitOrderCommand) -> None:
# Orchestration only — no business decisions here
order = self._orders.find_by_id(command.order_id)
if order is None:
raise OrderNotFound(command.order_id)
order.submit() # domain logic lives here
payment = self._payments.authorize(order.total)
order.record_payment(payment.id) # still domain logic
self._orders.save(order)
self._events.publish(order.pop_events())
Rules:
if statements expressing business rules — those belong in domain objectsSubmitOrder, CancelSubscription, TransferFundsCommon violations:
if order.total > 1000: order.apply_bulk_discount() — business rule, belongs in OrderEncapsulates domain logic that does not naturally fit inside a single entity or value object — typically logic that involves multiple aggregates or requires domain knowledge that spans objects.
Pain it removes: domain logic that operates across multiple objects with no natural home in any single entity, leading to it being placed in application services where it does not belong.
When to use: the operation is meaningful to domain experts; it involves multiple aggregates; it has no natural home in a single entity; it is stateless.
When NOT to use: if the logic can naturally live in an entity or value object, put it there. Domain services should be the exception.
class FundsTransferService:
"""
Domain service — 'transfer funds' spans two Account aggregates.
Neither Account should know about the other.
"""
def transfer(
self,
source: Account,
destination: Account,
amount: Money
) -> None:
source.debit(amount) # enforces source invariants
destination.credit(amount) # enforces destination invariants
# No infrastructure here — that is the application service's responsibility
Naming: domain services are named after domain concepts and verbs, not technical operations. FundsTransferService not AccountUpdateService. PricingService not PriceCalculatorHelper.
Stateless: domain services hold no persistent state. If state is needed, it belongs in an entity.
The application's boundary — defines the set of operations the application exposes, coordinates domain objects in response to each, and presents a coherent interface to all callers (HTTP controllers, CLI commands, message consumers, background jobs).
Key insight: the Service Layer is deliberately thin. It is not where logic lives — it is where the application exposes its capabilities. All logic is delegated to the domain model.
class BillingService:
def charge_subscription(self, subscription_id: str) -> ChargeResult: ...
def cancel_subscription(self, subscription_id: str, reason: str) -> None: ...
def update_payment_method(self, subscription_id: str, token: str) -> None: ...
def apply_coupon(self, subscription_id: str, coupon_code: str) -> None: ...
Anti-pattern — Anemic Domain Model: when the service layer contains all logic and domain objects are pure data containers with getters and setters. This looks like OOP but is procedural code. The tell: every domain operation is a service method; domain objects have no behaviour.
if expressions encoding business rulesWhat SKILL.md sets; muted values are Claude Code's defaults
10 skills in this plugin
/anti-corruptionUse when external system types, vocabulary, or conventions bleed into your domain model, a single provider change would require touching many internal files, or a legacy system must keep running while a new one is built alongside it. Apply before writing any integration with an external API, legacy codebase, or third-party service. Covers Anti-Corruption Layer, Gateway, Facade, and Strangler Fig./behaviouralUse when behaviour varies across cases, accumulates conditionals, or needs to be decoupled, queued, undone, or observed. Apply when a method is growing if/switch branches, an algorithm differs per context, a pipeline needs flexible steps, or objects need to react to each other without tight coupling. Covers all 10 GoF behavioural patterns./cqrsUse when read and write performance requirements diverge significantly, complex reporting is painful through the domain model, or you need to scale reads and writes independently. Apply before splitting any command/query handler or building read-optimised projections. Covers Command Query Responsibility Segregation — including when it is overkill — with guidance on applying only as much separation as the problem demands./creationalUse when object creation is the source of pain — constructors growing too complex, the same setup repeated, unclear which concrete type to instantiate, or a shared instance causing hidden coupling. Apply before writing any factory, builder, or instantiation logic. Covers all 5 GoF creational patterns: Singleton, Builder, Factory Method, Abstract Factory, Prototype./data-accessUse when persistence logic bleeds into domain or business code, tests require a real database to verify business logic, ORM types appear in service or handler code, or switching storage would require touching domain classes. Apply before writing any database query, repository, or ORM integration. Covers Repository, Unit of Work, Data Mapper, and Active Record with explicit guidance on when each applies./domain-eventsUse when aggregates need to stay loosely coupled, side effects (emails, audit logs, projections) are tangled into domain operations, or one aggregate directly calls methods on another. Apply when something significant happens in the domain that other parts of the system need to react to without being tightly coupled. Covers Domain Events, publishing patterns, synchronous vs asynchronous handling, idempotency, and eventual consistency trade-offs.npx claudepluginhub entelligentsia/skillforge --plugin design-patternsOther plugins' skills for the same job