I want to use DAO, but how can I inject the returned dependency objects correctly
Ok, so I have a dependency injection and DAO container that I use, for example, to get the order, like:
$container = new DIContainer();
$orderDAO = $container->get('orderDAO');
$order = $orderDAO->fetchById($someId);
and then I have an order object that is easy to use.
The point is, if my object $order
has dependencies on Logger
, Config
and one or two more such objects, since mine $orderDAO
creates an instance of the object, it shouldn't be able to access or be able to create these additional objects, and I'm sure the object $orderDAO
should definitely know nothing about these additional objects, especially don't know how to create them.
I know that I could inject a dependency injection container into the DAO when it is instantiated (from within the DIC), and that way I will have access to whatever dependency my objects have inside the DAO, but something in that regard to me for some reason- I don't like that, and I definitely don't want to make static calls all over the place, so the method exits the window.
What's the best way to do this?
Any help would be greatly appreciated.
source to share
Letting the DI container manage the internal dependencies of the objects, like assigning a Logger, Config and the like, that inside the object you create is the usual way of doing things. If for some reason you don't want to allow the use of a DI container, then you can simply create a default constructor and assign these values to it.
In fact, it looks like you need to put in some infrastructure stuff, so it's best to keep it as simple as possible, without any additional stuff, because it will lead you to unnecessary complexity.
UPD:
So productDAO doesn't have access to Config etc. But you want this product to have it. I think this is wrong from a design point of view. Since usually objects, the main purpose of which is stored, the data should not have any function other than its business logic. if you will not be registering and configuring, you must do it inside the DAO, but not in the product. But anyway, if you like, just wrap things up for things that could be changed in the future (say Logger) and then just assign that value in the constructor manually.
source to share