Ask Your Question
1

How is it possible to employ traits in order to create service layers that are not dependent on the repository level, while still making use of this trait?

asked 2021-05-10 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-05-18 21:00:00 +0000

bukephalos gravatar image

One way to achieve this is by implementing the Dependency Inversion Principle (DIP) and using Dependency Injection (DI) to inject the required repository implementation into the service layer via constructor injection or setter injection.

First, define a generic interface or trait for the repository level methods, such as:

interface UserRepositoryInterface {
    public function findById(int $id): ?User;
    public function findAll(): array;
    public function save(User $user): void;
}

Then, create a concrete implementation of this interface, such as:

class UserRepository implements UserRepositoryInterface {
    public function findById(int $id): ?User {
        // ...
    }

    public function findAll(): array {
        // ...
    }

    public function save(User $user): void {
        // ...
    }
}

Next, create a service layer class that uses the trait and requires the UserRepositoryInterface via DI:

class UserService {
    use UserRepositoryTrait;

    public function __construct(UserRepositoryInterface $userRepository) {
        $this->userRepository = $userRepository;
    }

    public function getUser(int $id): ?User {
        return $this->userRepository->findById($id);
    }

    // ...
}

Finally, configure the DI container to inject the UserRepository instance when creating a new UserService instance:

$container = new DI\Container();
$container->set(UserRepositoryInterface::class, DI\autowire(UserRepository::class));
$container->set(UserService::class, DI\autowire(UserService::class));

This way, the service layer is not dependent on the concrete repository implementation, but still uses the methods provided by the UserRepositoryInterface via the UserRepositoryTrait. This makes it easy to switch out the actual repository implementation with a different one if needed, without affecting the service layer.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-10 11:00:00 +0000

Seen: 9 times

Last updated: May 18 '22