Last active
April 13, 2023 12:45
-
-
Save juanwilde/96eb0c7376beb0eb5c97ab2c5c2c018c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coupled to framework (a service knowing about the implementation of the data layer) | |
class OrderService | |
{ | |
public function getOrders(): iterable | |
{ | |
return Order::all(); | |
} | |
} | |
// Not coupled to framework | |
// first an interface | |
interface OrderRepository | |
{ | |
public function all(): iterable; | |
} | |
// then the concrete implementation | |
final class EloquentOrderRepository implements OrderRepository | |
{ | |
public function all(): iterable | |
{ | |
return Order::all(); | |
} | |
} | |
// And then, use that interface in the service | |
class OrderService | |
{ | |
public function __construct( | |
private readonly OrderRepository $orderRepository | |
) | |
public function getOrders(): iterable | |
{ | |
return $this->orderRepository->all(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment