Factory to pass dynamic dependencies

11 days ago

dkorpar
Member | 47
+
0
-

Let's say I have payment methods, all of them implements interface IPaymentMethod.
I have a factory that creates them. But different payment methods require different dependencies.
How would I set in factory that each one is initialized with correct dependencies? Or calling inject* methods with proper parameters? I was thinking with reflection and having container on Factory, but I'm thinking is there maybe some cleaner way?

Last edited by dkorpar (2017-10-09 14:23)

11 days ago

CZechBoY
Member | 2644
+
+3
-

Register all as service in DIC and then in resolver get them via findByType+getService

9 days ago

dkorpar
Member | 47
+
0
-

What if we do not want to register them as services?

9 days ago

CZechBoY
Member | 2644
+
0
-

Then tell me WHY :-)

9 days ago

dkorpar
Member | 47
+
0
-

Payment method is wrong example here so ignore it, but what if you need multiple instances of same class?

8 days ago

GEpic
Member | 414
+
0
-

dkorpar wrote:

Payment method is wrong example here so ignore it, but what if you need multiple instances of same class?

If you need same class with different dependencies, than there is something wrong with your design.
Did you mean different arguments?

If you mean child classes of same parent, then you can, of course, have different dependencies and best way to do it is as @CZechBoY mentioned above.

Would be better to show some real example.

Last edited by GEpic (2017-10-12 01:46)

8 days ago

dkorpar
Member | 47
+
0
-

Hm, so we have ShippingMethods, and I need to get list off all shipping methods. Type which will be used for every shipping method is in db and it's possible that we have custom shipping1, custom shipping 2… and for all those shipping same class CustomShipping is being used. I don't want to have same instance for those…
I should probably then have factory for every type of shipping and have factory in db and not class…
Just wanted to see if I can stay clean somehow and skip that, there's already 300+ services registered :)

Last edited by dkorpar (2017-10-12 13:03)