Factory to pass dynamic dependencies

Notice: This thread is very old.
dkorpar
Member | 136
+
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)

CZechBoY
Member | 3608
+
+3
-

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

dkorpar
Member | 136
+
0
-

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

CZechBoY
Member | 3608
+
0
-

Then tell me WHY :-)

dkorpar
Member | 136
+
0
-

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

GEpic
Member | 566
+
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)

dkorpar
Member | 136
+
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)