Exercise 2: Adding save_refund() Behind the Existing Boundary — Possible Solution ==================================================================== THE NEW REPOSITORY METHODS ------------------------------ # FileOrderRepository def save_refund(self, order_id, amount): with open('refunds.txt', 'a') as f: f.write(f'{order_id},{amount}\n') # InMemoryOrderRepository def save_refund(self, order_id, amount): self.refunds.append(f'{order_id},{amount}') Each repository gets its own save_refund(), storage-specific exactly like this chapter's own save_order()/save_payment() pair - the file version writes to refunds.txt, the in-memory version appends to a new self.refunds list. THE NEW BUSINESS FUNCTION ------------------------------ def record_refund(repo, order_id, amount): repo.save_refund(order_id, amount) Matches this chapter's own record_order()/record_payment() shape exactly - it takes an injected repo and delegates, mentioning no storage technology at all. VERIFYING NO open() CALL IN THE NEW BUSINESS FUNCTION ------------------------------ record_refund contains open(): False Confirmed directly via inspect.getsource(), exactly like this chapter's own verification of the three original business functions. VERIFYING IT WORKS AGAINST BOTH REPOSITORIES ------------------------------ Calling record_refund(file_repo, 'ORD-1', 20) and record_refund(memory_repo, 'ORD-1', 20) with the identical business function: memory_repo.refunds: ['ORD-1,20'] refunds.txt contents: ['ORD-1,20'] Both repositories correctly recorded the refund using their own storage mechanism, and record_refund() itself never needed to know which one it was talking to. WHY THIS WORKS AS AN ANSWER ------------------------------ The new capability is added entirely within the existing boundary - one new method per repository, one new business function that only delegates - following this chapter's own established shape exactly, and both the "no open() call" claim and the "works against both repositories" claim are verified directly rather than assumed to follow from the pattern being followed correctly.