Reputation: 320
I read somewhere, IOC is different from factory pattern. As Factory design pattern is more intrusive, where as Dependency Injection is not.
Could someone elaborate more on this?
Upvotes: 5
Views: 1228
Reputation: 101150
Inversion of control containers is not primarily used for dependency injection. It's to let the container control the lifetime of your objects. Hence the inversion of control
.
That's why you always specify a lifetime when you register things in the container (or just use the default lifetime)
However, since the container creates the objects for you, it can also provide dependency injection as an extra feature. So it's really a bonus.
Factory pattern on the other hand should ALWAYS create a new object. The purpose of factory pattern is simply to create the correct implementation for you.
Upvotes: 2
Reputation: 93444
Yes, IoC and Factory are two different things. IoC is actually a more generic term, and many things qualify as IoC, so it helps to further refine what it is you are referring to. For example, technically, any callback or event is considered an implementation of IoC. Most people mean Dependency Injection when they talk about IoC, however.
You can use a Factory to achieve Inversion of Control, just like you can use Dependency Injection to achieve it.
What you're probably thinking of is an Dependency Injection container, like Unity, Windows, or Ninject. A DI container is sort of a glorified abstract generic factory, but it does a lot more than that, including object lifetime management, conditional binding, etc...
It's important to separate the pattern (IoC or DI) from the implementations (Factory, DI Container, Poor mans DI, etc..) even though the implementations may themselves also be patterns.
Upvotes: 3