Agent Wranglers

ARC-47Recommended

Depend on abstractions at the edges

Business rules depend on small interfaces they own. Databases, payment providers and email services implement those interfaces.

How to do it

Define the interface next to the code that uses it, in its words. Pass the implementation in through the constructor. Then tests can pass a fake, and a provider can change without touching the rules. Never create databases, clients or mailers inside business code. Model providers fit the same pattern, as Call models through one module says.

Examples

TypeScript example

// Bad: the checkout rule imports the payment vendor directly
import { VendorPay } from 'vendor-pay-sdk';

// Good: checkout owns the interface, the vendor adapter implements it
interface PaymentGateway { charge(amount: Money, token: string): Promise<Receipt>; }
class Checkout { constructor(private readonly payments: PaymentGateway) {} }

Python example

# Good in Python: checkout owns the protocol, the vendor adapter implements it
class PaymentGateway(Protocol):
    def charge(self, amount: Money, token: str) -> Receipt: ...

class Checkout:
    def __init__(self, payments: PaymentGateway):
        self.payments = payments

PHP example

// Good in PHP: the interface lives with the checkout code
interface PaymentGateway { public function charge(Money $amount, string $token): Receipt; }
final class Checkout { public function __construct(private PaymentGateway $payments) {} }

How to check

Business code lives in its own folder, such as domain/. An import rule fails when a file in that folder imports a vendor SDK. Examples are ESLint no-restricted-imports, dependency-cruiser, Deptrac and import-linter.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/solid-and-dry/dependency-inversion">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.