Dependency injection 控制反转与依赖注入

Dependency injection 控制反转与依赖注入,dependency-injection,inversion-of-control,Dependency Injection,Inversion Of Control,根据,控制反转是程序控制流反转的原理:外部源(框架、服务、其他组件)控制程序流,而不是程序员控制程序流。就好像我们把什么东西插进了别的东西。他提到了一个关于EJB2.0的示例: 例如,会话Bean接口 定义ejbRemove、ejbPassivate (存储到辅助存储器),以及 ejbActivate(从被动恢复) 国家)。你无法控制何时开始 这些方法叫做什么 是的。集装箱叫我们,我们 别这么说 这导致了框架和库之间的差异: 控制反转是控制系统的关键部分 是什么使框架不同于 图书馆。图书馆本质上

根据,控制反转是程序控制流反转的原理:外部源(框架、服务、其他组件)控制程序流,而不是程序员控制程序流。就好像我们把什么东西插进了别的东西。他提到了一个关于EJB2.0的示例:

例如,会话Bean接口 定义ejbRemove、ejbPassivate (存储到辅助存储器),以及 ejbActivate(从被动恢复) 国家)。你无法控制何时开始 这些方法叫做什么 是的。集装箱叫我们,我们 别这么说

这导致了框架和库之间的差异:

控制反转是控制系统的关键部分 是什么使框架不同于 图书馆。图书馆本质上是一个图书馆 可以调用的函数集, 这些日子通常被组织成 上课。每个电话都会做一些工作,并且 将控件返回给客户端

我认为,从DI是IOC的观点来看,意味着对象的依赖关系是颠倒的:不是它控制自己的依赖关系,而是生命周期。。。还有别的东西可以帮你。但是,正如你亲手告诉我的,DI不一定是国际奥委会。我们仍然可以有DI而没有IOC

然而,在本文中(来自pococapsule,C/C++的另一个IOC框架),它认为由于IOC和DI,IOC容器和DI框架远远优于J2EE,因为J2EE将框架代码混合到组件中,因此它不是简单的旧Java/C++对象(POJO/POCO)

(档案链接)

额外阅读以了解旧的基于组件的开发框架存在什么问题,从而引出上面的第二篇文章:(归档链接)


我的问题:国际奥委会和国际奥委会到底是什么?我很困惑。基于pococapsule,IOC不仅仅是对象或程序员与框架之间的控制反转,它更重要。

IOC是一个通用术语,意思是应用程序调用库(也称为toolkit)提供的实现,框架调用应用程序提供的实现

DI是IoC的一种形式,在IoC中,实现通过构造函数/设置器/服务查找传递到对象中,对象将“依赖”这些构造函数/设置器/服务查找,以便正确运行

例如,不使用DI的IoC将是模板模式,因为实现只能通过子分类进行更改

DI框架旨在利用DI,并可以定义接口(或Java中的注释),以便于在实现中传递

IoC容器是可以在编程语言之外工作的DI框架。在某些情况下,您可以配置在入侵性较小的元数据文件(如XML)中使用哪些实现。有了一些,你可以做IoC,这通常是不可能的,比如在一个服务器上注入一个实现


另见此。

简而言之,国际奥委会是一个更广泛的术语,包括但不限于直接投资

术语“控制反转”(InversionofControl,IoC)最初指的是任何一种编程风格,其中 框架或运行时控制程序流

在DI命名之前,人们开始将管理依赖关系的框架称为反转 关于控制容器,很快,IoC的含义逐渐向那个特定的含义漂移:控制依赖的反转

控制反转(IoC)意味着对象不会创建其工作所依赖的其他对象。相反,它们从外部源(例如,xml配置文件)获取所需的对象


依赖注入(DI)意味着这是在没有对象干预的情况下完成的,通常由传递构造函数参数和设置属性的框架组件完成。

IOC表示管理应用程序类的外部类,外部类意味着容器管理应用程序类之间的依赖关系。 IOC的基本概念是程序员不需要创建对象,而是描述应该如何创建对象

IoC容器执行的主要任务是: 实例化应用程序类。来配置对象。组装对象之间的依赖关系


DI是通过使用setter注入或构造函数注入在运行时提供对象依赖关系的过程。

但是spring文档说它们是相同的


在第一行“IoC也称为依赖注入(DI)”。

DI是IoC的一个子集

  • IoC意味着对象不会创建其工作所依赖的其他对象。相反,他们从外部服务(例如,xml文件或单个应用程序服务)获取所需的对象。我使用的两种IoC实现是DI和ServiceLocator
  • DI意味着获取依赖对象的IoC原则是不使用具体对象而使用抽象(接口)。这使得所有组件都可以链式测试,因为更高级别的组件不依赖于较低级别的组件,而只依赖于接口。mock实现这些接口

.

IoC-控制反转是一个通用术语,与语言无关,它实际上不是创建对象,而是描述创建对象的方式


DI-依赖项注入是一个具体的术语,在这个术语中,我们使用不同的注入技术,即。Setter注入、Constructor注入或接口注入。

控制反转是一种设计范例,其目标是提供更多的控制
public class SMSService
{
    public void SendSMS(string mobileNumber, string body)
    {
        SendSMSUsingGateway(mobileNumber, body);
    }

    private void SendSMSUsingGateway(string mobileNumber, string body)
    {
        /*implementation for sending SMS using gateway*/
    }
}

public class UIHandler
{
    public void SendConfirmationMsg(string mobileNumber)
    {
        SMSService _SMSService = new SMSService();
        _SMSService.SendSMS(mobileNumber, "Your order has been shipped successfully!");
    }
}
public interface ISMSService
{
    void SendSMS(string phoneNumber, string body);
}
public class SMSService : ISMSService
{
    public void SendSMS(string mobileNumber, string body)
    {
        SendSMSUsingGateway(mobileNumber, body);
    }

    private void SendSMSUsingGateway(string mobileNumber, string body)
    {
        /*implementation for sending SMS using gateway*/
        Console.WriteLine("Sending SMS using gateway to mobile: 
        {0}. SMS body: {1}", mobileNumber, body);
    }
}
public class MockSMSService :ISMSService
{
    public void SendSMS(string phoneNumber, string body)
    {
        SaveSMSToFile(phoneNumber,body);
    }

    private void SaveSMSToFile(string mobileNumber, string body)
    {
        /*implementation for saving SMS to a file*/
        Console.WriteLine("Mocking SMS using file to mobile: 
        {0}. SMS body: {1}", mobileNumber, body);
    }
}
public class UIHandler
{
    public void SendConfirmationMsg(string mobileNumber)
    {
        ISMSService _SMSService = new MockSMSService();
        _SMSService.SendSMS(mobileNumber, "Your order has been shipped successfully!");
    }
}
public class UIHandler
{
    private readonly ISMSService _SMSService;

    public UIHandler(ISMSService SMSService)
    {
        _SMSService = SMSService;
    }

    public void SendConfirmationMsg(string mobileNumber)
    {
        _SMSService.SendSMS(mobileNumber, "Your order has been shipped successfully!");
    }
}
class Program
{
    static void Main(string[] args)
    {
        ISMSService _SMSService = new MockSMSService(); // dependency

        UIHandler _UIHandler = new UIHandler(_SMSService);
        _UIHandler.SendConfirmationMsg("96279544480");

        Console.ReadLine();
    }
}
Public MyClass{
 DependentClass dependentObject
 /*
  At somewhere in our code we need to instantiate 
  the object with new operator  inorder to use it or perform some method.
  */ 
  dependentObject= new DependentClass();
  dependentObject.someMethod();
}
Public MyClass{
 /* Dependency injector will instantiate object*/
 DependentClass dependentObject

 /*
  At somewhere in our code we perform some method. 
  The process of  instantiation will be handled by the dependency injector
 */ 

  dependentObject.someMethod();
}
public class  AuditDAOImpl implements Audit{

    //dependency
    AuditDAO auditDAO = null;
        //Control of the AuditDAO is with AuditDAOImpl because its creating the object
    public AuditDAOImpl () {
        this.auditDAO = new AuditDAO ();
    }
}
public class  AuditDAOImpl implements Audit{

    //dependency

     //Now control is shifted to Spring. Container find the object and provide it. 
    @Autowired
    AuditDAO auditDAO = null;

}
   DI(Dependency Injection):  Way of injecting properties to an object is 
   called 
  Dependency injection.
   We have three types of Dependency injection
    1)  Constructor Injection
    2)  Setter/Getter Injection
    3)  Interface Injection
   Spring will support only Constructor Injection and Setter/Getter Injection.
//DI
child.getSchool();
//IOC
child.perform()// is a stub implemented by dance-school
child.flourish()// is a stub implemented by dance-school/school/
print ("Please enter your name:");
scan (&name);
print ("Please enter your DOB:");
scan (&dob);

//More print and scan statements
<Do Something Interesting>

//Call a Library function to find the age (common code)
print Age
Class TextEditor
{

    //Lot of rocket science to create the Editor goes here

    EnglishSpellChecker objSpellCheck;
    String text;

    public void TextEditor()

    {   

        objSpellCheck = new EnglishSpellChecker();

    }

    public ArrayList <typos> CheckSpellings()
    {

        //return Typos;

    }

}
interface ISpellChecker
{

    Arraylist<typos> CheckSpelling(string Text);

}

Class EnglishSpellChecker : ISpellChecker

{

    public override Arraylist<typos> CheckSpelling(string Text)

    {

        //All Magic goes here.

    }

}



Class FrenchSpellChecker : ISpellChecker

{

    public override Arraylist<typos> CheckSpelling(string Text)

    {

        //All Magic goes here.

    }

}
Class TextEditor

{

    ISpellChecker objSpellChecker;

    string Text;



    public void TextEditor(ISpellChecker objSC)

    {

        objSpellChecker = objSC;

    }



    public ArrayList <typos> CheckSpellings()

    {

        return objSpellChecker.CheckSpelling();

    }

}
// dependency injection
function doSomething(dependency) {
    // do something with your dependency
}

// in contrast to creating your dependencies yourself
function doSomething() {
    dependency = getDependencySomehow()
}

// inversion of control
application = makeApp(authenticate, handleRequest, sendResponse)
application.run(getRequest())

// in contrast to direct control or a "library" style
application = makeApp()
request = application.getRequest()

if (application.authenticate(request.creds)) {
    response = application.handleRequest(request)
    application.sendResponse(response)
}
class A {
  B b

  //injecting B via constructor 
  init(b: B) {
     self.b = b
  }
}
class A {
  IB ib

  init(ib: IB) {
     self.ib = ib
  }
}
interface InterfaceInputPort {
    func input()
}

interface InterfaceOutputPort {
    func output()
}

class A: InterfaceOutputPort {

    let inputPort = B(outputPort: self)

    func output() {
        print("output")
    }
}

class B: InterfaceInputPort {
    let outputPort: InterfaceOutputPort

    init(outputPort: InterfaceOutputPort) {
        self.outputPort = outputPort
    }

    func input() {
        print("input")
    }
}