Briefle words · Code & architecture
Dependency injection
Passing a component the things it depends on from outside, instead of letting it create them itself, so they can be swapped, for example in tests.
Say it to an AI
Python RefundService using dependency injection: pass the PaymentGateway and a clock callable into the constructor instead of creating Paylane or calling datetime.now() inside, so tests can use a fake gateway and a fixed time. Raise RefundWindowClosed past the deadline.
How an expert would brief the stronger version of “Refund service”.
See it
Try it as a puzzle →A fintech backend refunds card payments, but only within the refund window.
Why: A's '__init__(self, payments, clock=datetime.now)' receives both; B's '__init__' creates 'PaylaneGateway(...)' on its own.
Related words
Room 3 · Functions & structure
Learn “Dependency injection” in a five-minute lesson