I have a situation where, during run time I need to instantiate three or more instances of object a through factory. I am pasting a snapshot, which describes it better.
Is there a better approach? Am I doing dependency injection correctly
Thanks
I have a situation where, during run time I need to instantiate three or more instances of object a through factory. I am pasting a snapshot, which describes it better.
Is there a better approach? Am I doing dependency injection correctly
Thanks
It is difficult to say whether you are doing dependency injection correctly without seeing any code, however it looks like you have a design that would definitely support dependency injection (as any class/object hierarchy should).
To implement this design, I would definitely look to inject the object creation factory into instances of class 1. This means that you are able to test the logic for class 1 in isolation from the factory logic by passing in a mock factory for your unit tests around class 1, and asserting that class 1 is calling the correct methods on your factory in these tests. Your design states that the factory is passed into class 1, and therefore it appears you are indeed doing things correctly. Of course, there should be a separate set of unit tests around the factory to test it is instantiating objects a1, a2 and a3 correctly.
Hope this helps.
Using an Abstract Factory for resolving run-time dependencies is a well-known solution.
Depending on circumstances, Metadata Role Hint, Role Interface Role Hint, or Partial Type Name Role Hint (my favourite) may be more elegant. solutions.