Consider the following class structure:
Framework code (blue zone)
- LOG class - simple logging class.
- DATABASE class - gets LOG class injected. (for lets say, logging errors or SQL queries).
- REDIS class - also gets LOG glass injected.
- BUSINESS logic classes - gets all of those above constructor injected.
.
Application code: (yellow zone):
- Instantiate and use the framework code.
Current framework constructor code is something like:
class Database{
public function __constuctor (Logger $log, $databaseHost, $databaseUser, $databasePass, $databaseName){...}
}
class Redis{
public function __constuctor (Logger $log, $redisHost, $redisUser, $redisPass){...}
}
class ACME{
public function __constuctor (Database $db, Redis $redis, $otherStuff, ...){...}
}
In the application we do something like:
$logger = new Logger();
$db = new Database($logger, 'db_host','db_user','db_user','db_pass');
$redis = new Redis($logger, 'redis_host','redis_user','redis_pass');
$acme = new Acme($db,$redis, ...other_stuf...);
The constructors gets pretty long and ugly and I am tempted to create a dependency container (DI) and to pass it as a single constructor parameter. .
Consider the principle: "The object should not be aware of the dependency container which contains it", I feel that is not OK, because this will just replace the current multiple dependencies with a new one - the dependency container (DIC) itself.
What I feel good is that I can use the injection of the DIC in the Application section (the yellow part of the image), but its NOT OK to inject the DIC in the base framework code (blue part).
Am I wrong?
Is it OK to inject the DIC?
Isn't it better to use a service locator pattern here?