mardi 26 avril 2016

(Laravel) Dynamic dependency injection for interface, based on user input

I am currently facing a very interesting dilemma with my architecture and implementation.

I have an interface called ServiceInterface which have a method called execute()

Then I have two different implementations for this interface: Service1 and Service2, which implements the execute method properly.

I have a controller called MainController and this controller has a "type-hint" for the ServiceInterface (dependency injection), it means that both, Service1 and Service2, can be called as resolution for that dependency injection.

Now the fun part:

I do not know which of those implementations to use (Service1 or Service2) because I just know if I can use one or other based on a user input from a previous step.

It means the user choose a service and based on that value I know if a can use Service1 or Service2.

I am currently solving the dependency injection using a session value, so depending of the value I return an instance or other, BUT I really think that it is not a good way to do it.

Please, let me know if you faced something similar and, how do you solve it, or what can I do to achieve this in the right way.

Thanks in advance. Please let me know if further information is required.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire