Understanding Inversion of Control and Dependency Injection

I am learning the concept of IoC and DI. I checked a few blogs and below is my understanding:

A tight-coupling example without the use of IoC:

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

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

After 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;
   }
}

The method PerformOperation in Class A is wrong. I guess, it is again tightly coupled as parameter is hard coded as B b.

In the above example, where is IoC as I can see only Dependency Injection in the constructor of Class A.

Please clear my understanding.


Your "After IoC" example is really "After DI". You are indeed using DI in class A. However, you seem to be implementing an Adapter pattern on top of DI which really isn't necessary. Not to mention, you are calling .Sum with only one argument from class A when the interface requires 2 arguments.

How about this? There a quick intro to DI at Dependency Injection - Introduction

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();
}

Well in simple words, IOC is a concept and DI is an implementation of it.

visit this post for more details, Inversion of Control vs Dependency Injection

链接地址: http://www.djcxy.com/p/82190.html

上一篇: 我如何让对象知道它在哪个容器中?

下一篇: 了解控制和依赖注入的反转