I’m currently coding a small web apps ( using SLIM 4) for reserving resources.
I’m using PHP-DI for the container. Dependencies are defined in the container. My routes points to single action classes. I inject the needed dependencies in the constructor.
It works well but …
I have some entities and i want to use the builder design pattern. The building rules are defined in different classes. One builder is injected in the director that orchestrate the building process.
Depending on the route/action and the context, i want to be able to choose which builder to inject in the director. I don’t find a good way to achieve these using the dependency injection.
Do i have to define all the director+builder combinations in the container and inject the desired one in the constructor ? Injecting the container would be an anti-pattern … I could manually instantiate the director and builder in the “controllers” but that would be inefficient.
One solution would be to inject both an empty director and the builder in the controller. Then associate both at runtime …
There must be a better solution ? Any clue welcome !
(PS: one good point is that searching for this question points me to " Refactoring to Aggregate Services" which is quite intersting.)