-3

My app is using this kind of layered architecture:

Controller > Service > Repository > Data Mapper > Persistence

Often I notice that my service methods are just directly calling repository methods without "doing anything else" to them. E.g.,

Class UserRepo
{
    // ...

    public function getUser($id)
    {
        return $this->dataMapper->findOne($id);
    }
}

class UserService
{
    // ...

    public function getUser($id)
    {
        return $this->userRepo->getUser($id);
    } 
}

I notice this kind of scenario is prevalent all throughout the app, where the service method doesn't do anything special and just calls the repository method. Although, there are still some cases where the service method actually does some business logic before it calls the repo.

Now I thought I could some time by ditching the repository pattern and just inject the data mapper directory to the service classes. E.g.,

class UserService
{
    // ...

    public function getUser($id)
    {
        return $this->dataMapper->findOne($id);
    } 
}

My question is, is it acceptable to ditch the repository pattern and just use the data mapper directly in the service class?

  • Yes, totally acceptable, why not? – Rune Jan 26 at 20:01
  • or the datamapper or the service – Ewan Jan 26 at 20:17
  • Down-votes, what's wrong with the question so I can rephrase it, thanks. – IMB Jan 27 at 2:16
  • ask a question on Reddit, it is better. – danilo Jan 30 at 1:45
2

You can't "ditch the repository pattern" if all your classes are repositories despite the naming.

Here it looks like both your datamapper and service are simply repositories. Flatten them out and call it a repository.

Or lose the service layer. If it has no logic you don't have a service.

The purpose of a layered design is to separate out parts of your code by responsibility. If your codes only responsiblity is to return domain objects from persistence. Then your code is a repository

|improve this answer|||||
  • Thanks for the feedback. If I remove the service layer how can services communicate with another service? – IMB Jan 27 at 2:19
  • in your example they dont – Ewan Jan 27 at 7:50
  • In the example I just tried to demonstrate where my concern is but the whole system communicate through services, so service X doesn't directly use repository Y. Given that, what do you recommend? – IMB Jan 27 at 9:31
  • in your example you could just put the repo in the controller – Ewan Jan 27 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.