Jump to content

Dependency Injection. It Works This Way But Something Tells Me I'm Doing It Wrong.


david91

Recommended Posts

In the application I am coding myself (MVC) I am using dependency injection to pass an object its dependencies. I don't want to use the Singleton pattern or a dependency injection container.

 

When the application starts up it has some procedural code instantiating important objects like this: (It loads up more objects than below but they should be enough to explain what I am on about)

 

/*
* ------------------------------------------------------
* INSTANTIATE THE APPLICATION CLASS
* ------------------------------------------------------
*/
$app = new App();
$app->environment('development');


/*
* ------------------------------------------------------
* INSTANTIATE THE CONFIGURATION CLASS
* ------------------------------------------------------
*/
$config = new Config();


/*
* ------------------------------------------------------
* INSTANTIATE THE LOGGER CLASS
* ------------------------------------------------------
*/
$logger = new Logger($config);


/*
* ------------------------------------------------------
* INSTANTIATE THE LOCALE CLASS
* ------------------------------------------------------
*/
$locale = new Locale();


/*
* ------------------------------------------------------
* INSTANTIATE THE HTTP RESPONSE CLASS
* ------------------------------------------------------
*/
$httpResponse = new HttpResponse();


/*
* ------------------------------------------------------
* INSTANTIATE THE ROUTER CLASS
* ------------------------------------------------------
*/
$router = new Router($config, $logger, $httpResponse);
$router->route();

 

As you can see the $logger object depends on the $config object and I inject the $config object into the $logger objects constructor.

 

Now, as you can see when I am instantiating the $router object it has 3 dependencies ($config, $logger and $httpResponse).

 

I just feel it's weird how the $logger object holds the $config object within it so anywhere I inject the $config object and the $logger object into an object like in the $router object I feel like I'm injecting the $config object twice and that it's wrong.

 

Obiously when I have more and more objects and dependencies it will seem like I am injecting the same object more than two or three times and that will really make me wonder if I'm doing it wrong.

 

Is what I'm doing ok?

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.