Most of the examples quoted for usage of Dependency Injection, we can solve using the factory pattern as well. Looks like when it comes to usage/design the difference between dependency injection and factory is blurred or thin.
Once someone told me that its how you use it that makes a difference!
I once used StructureMap a DI container to solve a problem, later on I redesigned it to work with a simple factory and removed references to StructureMap.
Can anyone tell me what is the difference between them and where to use what, whats the best practice here?
There are problems which are easy to solve with dependency injection which are not so easily solved with a suite of factories.
Some of the difference between, on the one hand, inversion of control and dependency injection (IOC/DI), and, on the other hand, a service locator or a suite of factories (factory), is:
IOC/DI is a complete ecosystem of domain objects and services in and of itself. It sets everything up for you in the way you specify. Your domain objects and services are constructed by the container, and do not construct themselves: they therefore do not have any dependencies on the container or on any factories. IOC/DI permits an extremely high degree of configurability, with all the configuration in a single place (construction of the container) at the topmost layer of your application (the GUI, the Web front-end).
Factory abstracts away some of the construction of your domain objects and services. But domain objects and services are still responsible for figuring out how to construct themselves and how to get all the things they depend on. All these "active" dependencies filter all the way through all the layers in your application. There is no single place to go to configure everything.
I had the same question as soon as I read about DI and ended up at this post. So finally this is what I understood but please correct me if am wrong.
"Long ago there were little kingdoms with their own governing bodies controlling and taking decisions based on their own written rules. Later on formed a big government eliminating all these little governing bodies which has one set of rules(constitution) and are implemented through courts"
The little kingdoms' governing bodies are "Factories"
The big government is the "Dependency Injector".
From: http://tutorials.jenkov.com/dependency-injection/dependency-injection-containers.html
With a factory you can group related interfaces, So If the parameters passed can be grouped in a factory then its also a good solution for
constructor overinjection
look at this code *):Look at the constructor, you only have to pass the
IAddressModelFactory
there, so less parameters *):You see in
CustomerController
a lot of parameters passed, Yes you can see this asconstructor overinjection
but this is how DI works. And no nothing is wrong with theCustomerController
.*) Code is from nopCommerce.
I think these are orthogonal and can be used together. Let me show you an example I recently came across at work:
We were using the Spring framework in Java for DI. A singleton class (
Parent
) had to instantiate new objects of another class (Child
), and those had complex collaborators:In this example,
Parent
has to receiveDepX
instances only to pass them to theChild
constructor. Problems with this:Parent
has more knowledge ofChild
than it shouldParent
has more collaborators than it shouldChild
involves changingParent
This is when I realized a
Factory
would fit here perfectly:Child
class, as seen byParent
Child
, which can be centralized in the DI configuration.This is the simplified
Parent
class and theChildFactory
class:Most answers here explain conceptual difference and implementation details of both. However I was unable to find explanation about difference in application which IMO is the most important and the OP asked about. So let me reopen this topic...
Exactly. In 90% cases you can obtain object reference using either Factory or DI and usually you end up with latter. In another 10% cases using Factory is only correct way. These cases include obtaining objects by variable at runtime parameters. Like this:
In this case getting
client
from DI is not possible (or at least requires some ugly workaround). So as a general rule to make decision: if a dependency can be obtained without any runtime calculated parameters then DI is preferred, otherwise use Factory.