Factory to pass dynamic dependencies
- dkorpar
- Member | 136
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)
- GEpic
- Member | 566
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
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)