4

I am working on a team where my mates introduced the service & repository pattern. We work on Lumen micro framework. So from the controller, the call is passed to the service and then to the repository.

We use controller injection. The service and transformer are injected into the controller's constructor. The service has the repository instantiated in its constructor. We are to follow this rule:

Route -> Controller (Service & Transformer) -> Service(Repository) -> Repository(Model).

The values inside the parentheses mean that they will be initialized inside the constructor.

Now, sometimes it is required to use another repository from the current one. Suppose I am on posts and I need to call user. What would be the way to go? Some say we should inject the user repository into the current repository (post). I disagreed because I don't think we should instantiate the user repository when it is not needed. Rather I would pass the user repository from Controller -> Service -> Repository. Would that be right?

Code:

  • Without passing the repository forward: Paste 1
  • Passing repository to next: Paste 2
Martin Maat
  • 18,218
  • 3
  • 30
  • 57
ssi-anik
  • 149
  • 5
  • 5
    My head hurts... – Robert Harvey Mar 13 '17 at 16:47
  • @RobertHarvey, Is it ok now? Someone made a change & was not approved. I had changed it now. Please, have a look. :) – ssi-anik Mar 13 '17 at 17:02
  • 5
    If I see the word repository one more time I'm going to go postal. – whatsisname Mar 13 '17 at 17:12
  • 2
    You should evaluate each approach on it merits (i.e. which approach best meets your requirements), and make the decision yourself. – Robert Harvey Mar 13 '17 at 17:27
  • 4
    Why do you need one repository to call another repository? Coordination of across repositories should probably be dealt with at a different level in the architecture, e.g. at the service layer. – John Wu Mar 13 '17 at 18:21
  • @JohnWu, As Laravel, resolves the dependency by itself thus all the services & repositories are instantiated at the controller level. passed to services for that action. And the services passes the 'required repositories' to called repository. I might be unable to express what I am talking about. – ssi-anik Mar 14 '17 at 05:00
  • 1
    To me it sounds like you need a new aggregate root for a new context. Then hydrate your new root model with a brand new repository. This model will have all required data on it already. This is ideal. In reality if I had to choose I would inject 2 repositories into the service and orchestrate the behaviour there. – Reasurria Jun 15 '17 at 08:53
  • 1
    The body of your question seems to have little or nothing to do with the title. – Paul Jan 04 '19 at 16:23

1 Answers1

1

I don't think that one repository should call another repository directly. Your service knows which repositories to deal with. So it would be clean if controllers deals with 2 services: UserService & PostService. It does something with UserService and then with PostService. Since Users and Posts are 2 different domains, it would be cleaner for them to live independently. This also lets you to decouple things. In future you may need few more repositories and you don't want to pollute your UserService with multiple repositories.

If you don't want different services, then I would inject UserRepsitory and PostsRepository to UserService thru dependency injection and let UserService take care of calling multiple repositories instead of one repository calling other repository.

Reading this would help: How accurate is "Business logic should be in a service, not in a model"?