George Irimiciuc
George Irimiciuc

Reputation: 4633

Should we inject a service parameter in both a child and parent service or create a method to return it from parent service

I have a few services, one, which has some variables that I need, is injected in two services, and these two are injected into another service, and I need to use a parameter from config.yml which is passed in the bottom one's constructor, into the last service.

vb_logger:
    class: BlotBundle\VBLogger\VBLogger
    arguments: ["@vb_members_module","@vb_posts_module"]
vb_forum_functions:
    class: BlotBundle\VBLogger\VBForumFunctions
    arguments: [%website%, %username%, %password%]
vb_members_module:
    class: BlotBundle\VBLogger\modules\Module_members
    arguments: [@vb_forum_functions]
vb_posts_module:
    class: BlotBundle\VBLogger\modules\Module_posts
    arguments: [@vb_forum_functions]

Here are my services. As you can see, I'm injecting %website%, %username%, %password% in vb_forum_functions and I need to access them in vb_logger. vb_forum_functions is injected in both vb_members_module and vb_posts_module. Then, I inject these two in vb_logger

To access my variables in vb_logger I need to create methods in vb_forum_functions that returns them, and then do something like

public function __construct(Module_members $module_members, Module_posts $module_posts)
{
    $this->vbff = $module_members->getVbff();

    $this->module_members = $module_members;
    $this->module_posts = $module_posts;
}

And in Module_members create a method that retrieves vb_forum_functions to which my variables were injected in.

protected $vbff;

public function __construct(VBForumFunctions $vbff) {
    $this->vbff = $vbff;
}

public function getVbff()
{
    return  $this->vbff;
}

Finally, in vb_logger I can get my variables like so:

 $this->password= $module_members->getVbff()->getPassword();

This sounds like a lot of work and bonus methods, but makes a bit more sense than to inject parameters again directly into the service, especially because they are already injected through both the two modules classes, that have vb_forum_functions injected into them, which already has the variables.

The alternative is

vb_logger:
    class: BlotBundle\VBLogger\VBLogger
    arguments: ["@vb_members_module","@vb_posts_module",%website%, %username%, %password%]
vb_forum_functions:
    class: BlotBundle\VBLogger\VBForumFunctions
    arguments: [%website%, %username%, %password%]

But again, the variables are already available two levels below, is it worth injecting them again?

Also, if vb_logger doesn't use my modules then, haven't I injected them in an improper manner? Because they also create a vb_forum_functions object each themselves, which will also be useless because it isn't used, in the case I use only one module. Even so, I can access this object through one of the modules, so the other doesn't have to inject it in vb_logger.

In short:

  1. How to access a service's variables(vb_forum_functions) into a service(vb_logger) into which is injected two other services(the two modules) that each have vb_forum_functions injected into?

  2. How to make it so that only the module used in vb_logger is instantiated, to avoid the other one that also creates a pointless vb_forum_functions object himself.

Upvotes: 4

Views: 281

Answers (1)

Tim B
Tim B

Reputation: 41188

By storing information not relevant to one service and publishing it to be used by another one you are reducing the encapsulation of those services and increasing how tightly coupled they are.

For example in the future if you changed the parent service to no longer need the injected thing then you would not be able to delete it without also changing everything that accesses it from outside.

If something is not related to the core responsibility of the service then in general it's a bad idea to add it (Single Responsibility Principle) unless the overhead of not doing so would be considerable.

In this case just inject things where you need them and keep your interfaces clean.

Upvotes: 1

Related Questions