【问题标题】:Understanding Inversion of Control and Dependency Injection了解控制反转和依赖注入
【发布时间】:2014-12-21 14:56:57
【问题描述】:

我正在学习 IoC 和 DI 的概念。我查了一些博客,以下是我的理解:

不使用 IoC 的紧耦合示例:

Public Class A
{
     public A(int value1, int value2)
     {
    return Sum(value1, value2);
     }

     private int Sum(int a, int b)
     {
    return a+b;
     }
}     

IoC 之后:

Public Interface IOperation
{
    int Sum(int a, int b);
}

Public Class A
{ 
     private IOperation operation;

     public A(IOperation operation)
     {
    this.operation = operation;
     }

     public void PerformOperation(B b)
     {
    operation.Sum(b);
     }
}

Public Class B: IOperation
{
   public int Sum(int a, int b)
   {
       return a+b;
   }
}

A 类中的 PerformOperation 方法错误。我猜,它又是紧密耦合的,因为参数被硬编码为 B b。

在上面的例子中,IoC 在哪里,因为我只能看到 Class A 的构造函数中的依赖注入。

请澄清我的理解。

【问题讨论】:

    标签: c# design-patterns dependency-injection inversion-of-control


    【解决方案1】:

    您的“After IoC”示例实际上是“After DI”。您确实在 A 类中使用了 DI。但是,您似乎在 DI 之上实现了一个适配器模式,这实际上是没有必要的。更不用说,当接口需要 2 个参数时,您仅使用 A 类的一个参数调用 .Sum。

    这个怎么样? Dependency Injection - Introduction 有一个关于 DI 的快速介绍

    public interface IOperation
    {
      int Sum(int a, int b);
    }
    
    private interface IInventoryQuery
    {
      int GetInventoryCount();
    }
    
    // Dependency #1
    public class DefaultOperation : IOperation
    {
      public int Sum(int a, int b)
      {
        return (a + b);
      }
    }
    
    // Dependency #2
    private class DefaultInventoryQuery : IInventoryQuery
    {
      public int GetInventoryCount()
      {
        return 1;
      }
    }
    
    
    // Dependent class
    public class ReceivingCalculator
    {
      private readonly IOperation _operation;
      private readonly IInventoryQuery _inventoryQuery;
    
      public ReceivingCalculator(IOperation operation, IInventoryQuery inventoryQuery)
      {
        if (operation == null) throw new ArgumentNullException("operation");
        if (inventoryQuery == null) throw new ArgumentNullException("inventoryQuery");
        _operation = operation;
        _inventoryQuery = inventoryQuery;
      }
    
      public int UpdateInventoryOnHand(int receivedCount)
      {
        var onHand = _inventoryQuery.GetInventoryCount();
        var returnValue = _operation.Sum(onHand, receivedCount);
    
        return returnValue;
      }
    }
    
    // Application
    static void Main()
    {
      var operation = new DefaultOperation();
      var inventoryQuery = new DefaultInventoryQuery();
      var calculator = new ReceivingCalculator(operation, inventoryQuery);
    
      var receivedCount = 8;
      var newOnHandCount = calculator.UpdateInventoryOnHand(receivedCount);
    
      Console.WriteLine(receivedCount.ToString());
      Console.ReadLine();
    }
    

    【讨论】:

      【解决方案2】:

      简单来说,IOC 是一个概念,DI 是它的实现。

      访问此帖子了解更多详情,Inversion of Control vs Dependency Injection

      【讨论】:

        猜你喜欢
        • 2012-02-05
        • 2010-10-31
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 2015-01-09
        • 1970-01-01
        • 2015-07-30
        相关资源
        最近更新 更多