Dynamic dependencies?

Hi there,
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 !
Thanks !
(PS: one good point is that searching for this question points me to " Refactoring to Aggregate Services" which is quite intersting.)

What exactly do you mean with “entities”? Can you please give more context?

Sorry, i was away in real life, busy painting a wood hut … :tent:

By entities, i mean classes that represents the some core object of my domain. Things that have identity and that i want to persist.

I deal with Booking. So i have a class called Reserver that represents a Booking.

I wanted to delegate the creation of these object to a Builder and a Director.
The Buider is injected in the Director and i have several builder that i want to use depending on the context.

My question is: " What would the correct way to deal with the injection of these two objects in the classes that needs them ?"

I could define in the container all the combinations but when i add a builder in the system, i’ll have to register all the new combinations … :unamused:

To make it work i just inject a builder and a director when needed, i just compose them after.

So is there a more efficient way to deal with this situation ?
Perhaps some PHP DI specific facilities ?

Thanks again

Ps: i could provide some code but i don’t know if it will make it clearer ? It’s more a question of architecture ( or design pattern ?)

If understand you correctly I think that this kind of “entities” should not be created using an IoC container.

PS: You could try to use a Factory.
PS 2: I think a tiny code snipped would help us to help.