开发者

Difference between ioc and dependency injection

Difference between ioc and dependency injection . explain dependency injection in spring. What is开发者_开发问答 difference b/w JSF dependency injection and spring dependency injection..


IoC means Inversion of Control.

Let’s see some “strongly coupled code” (“MyComponent” depends on “Logger”):

public class MyComponent
{
  public MyComponent()
  {
  :
  }
  public void DoSomeWork()
  {
    var logger = new Logger();
  :
  }
}

We can change it to use an “interface“, but someone must provide the “implementation“:

public class MyComponent
{
  public MyComponent()
  {
  :
  }
  public void DoSomeWork()
  {
    ILogger logger = ...; // who’s going to provide this?
  :
  }
}

Dependency Injection (DI) is a specific implementation of IoC.

//Dependency Injection pattern
public class MyComponent
{
  private ILogger _logger;
  public MyComponent(ILogger logger)
  {
    _logger = logger;
  }
  public void DoSomeWork()
  {
    // Use the logger component here
    _logger.Log();
   :
  }
}

Another implementation is Service Locator.

//Service Locator pattern
public class MyComponent
{
    public MyComponent()
    {
        :
    }
    public void DoSomeWork()
    {
        ILogger logger = ServiceLocator.GetService();
        :
    }
}

Martin Fowler states: “With service locator the application class asks for it explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class – hence the inversion of control.”

Also : “The choice between Service Locator and Dependency Injection is less important than the principle of separating service configuration from the use of services within an application. “

You can check this post: Dependency Inversion: Service Locator or Dependency Injection

Also:

ASP.NET MVC: Resolve or Inject? That’s the Issue… by Dino Esposito

Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler

InversionOfControl by Martin Fowler


The terms Dependency Injection (DI) & Inversion of Control (IoC) are generally used interchangeably to describe the same design pattern (although not everyone agrees on that point, and some people tend to apply them in slightly different ways). The pattern was originally called IoC, but Martin Fowler proposed the shift to DI because all frameworks invert control in some way and he wanted to be more specific about which aspect of control was being inverted.

This article on Dependency Injection provides an overview and references the Fowler article. It's also less specific to particular technologies like Spring & PicoContainer, and provides links to some other interesting articles on the topic. Agree with the above poster though, the Fowler article is the canonical read on the subject and gives a great overview as well.


IoC is a design pattern, and most IoC containers use dependency injection. There are different types of dependency inection, but best read a few of these articles which helped me;

CodeProject

IoC Container

Windsor Container

Beginner guide


See Dependency Injection in .NET by Mark Seemann.


The best article you will find on this is by Martin Fowler at http://martinfowler.com/articles/injection.html . Regarding JSF, this article http://java.dzone.com/articles/jsf-anti-patterns-and-pitfalls, seems to indicate that the difference is that JSF uses Setter Injection as opposed to Constructor Injection (which is generally preferred).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜