Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Factory to get Injected objects

Is it good practice to have a Factory method to retrieve injected objects or is it OK to just use the factory method from the DI framework?

I'm using structure map, should I just use ObjectFactory.GetInstance();, or should I create factory class and inside this class call ObjectFactory.GetInstance();? because if I call ObjectFactory.GetInstance(); in my classes I would be creating coupling to the DI framework? sorry if I'm being ignorant, I'm new to this concepts. Thanks!

like image 832
ryudice Avatar asked May 18 '10 17:05

ryudice


1 Answers

If you are already using a DI framework why re-implement the factory pattern when it is already provided by the framework? Also you shouldn't be creating a dependency with the DI framework in the business layers of the application. There you should be abstracting with interfaces and abstract classes. The DI framework should only be used only at the highest level, for example in the GUI to perform the plumbing of the lower layers and select for instance a suitable data access layer.

like image 199
Darin Dimitrov Avatar answered Sep 24 '22 10:09

Darin Dimitrov