1

The following is my IOperation interface which has two signatures :

public interface IOperations
{

    int Mul(int a, int b);
    int Sum(int a, int b);
}

In the Operation class i have implemented the aforementioned methods:

public class Operations:IOperations
{
  public  int Mul(int a,int b)
    {


        return a * b;
    }

  public  int Sum(int a,int b)
    {


        return a + b;
    }
}

Now in the main Program how should i meet the DI?like this?

    static void Main(string[] args)
    {
        IOperations myOperations = new Operations();

        myOperations.Mul(3, 2);


    }
mrslt
  • 337
  • 2
  • 10

2 Answers2

1

This is defining constructor injection, which is the most common DI pattern. You are basically saying that your TestController requires a class that implements IUnitOfWork in order to function. But instead of hardcoding a dependency, you specify that TestController will get passed an implementation of IUnitOfWork when it's created. Somewhere in the configuration of your dependency injection framework you will have something like:

Bind<IUnitOfWork >().To<UnitOfWorkImpl>();

which specifies the actual class that will be instantiated.

John M
  • 2,037
  • 5
  • 20
  • 27
1

That has nothing much to do with dependency injection; what the constructor of

public class TestController
{
    private IUnitOfWork unitOfWork;

    public TestController(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }
}

does is it simply takes an instance of a IUnitOfWork object and stores it in it's field unitOfWork.

So, you might (but shouldn't) call it like this:

// Returns an instance of IUnitOfWork.
IUnitOfWork mySpecificUnitOfWorkInstance = this.GetUnitOfWork();

// Now you pass that exact instance to the TestController so that it can do stuff with it.
TestController testController = new TestController(mySpecificUnitOfWorkInstance);

More simple example maybe (without the possibly confusing concepts of dependency injection, repository pattern etc.):

public class NumberHolder
{
    private int number = 0;

    public NumberHolder(int number)
    {
        this.number = number;
    }
}

and if you call it like this

NumberHolder foo = new NumberHolder(42);

you actually pass 42 to the new instance of NumberHolder.


Further reading: Using Constructors on docs.microsoft.com.

Thomas Flinkow
  • 3,956
  • 5
  • 23
  • 52
  • can i ask one more question regarding DI here? – mrslt Jun 04 '18 at 08:32
  • @mrslt sure, go ahead – Thomas Flinkow Jun 04 '18 at 08:32
  • I am trying to understand DI,i have created a small console project,created an Interface IOperations which has two signitures: int mul(int a, int b); int sum(int a, int b); – mrslt Jun 04 '18 at 08:36
  • I implemented the methods in Operations class,but according to DI how should i use them in main program to satisfy the DI – mrslt Jun 04 '18 at 08:38
  • 1
    There's nothing to keep in mind about dependency injection here, because **there are no dependencies**. If you were to create a `Calculator` class which needs to work with mathematical operations, the calculator should only ever "know" `IOperations` and not `Operations`. What that means is that the signature of the constructor should be like this `public Calculator(IOperations operations)` and **not** `public Calculator(Operations operations)`. – Thomas Flinkow Jun 04 '18 at 08:40
  • yes,but here i have a methos for mul,another method for sum,and i should see only IOperation,right?but my question is how to use the IOperation in the main program – mrslt Jun 04 '18 at 08:44
  • @mrslt I'm not sure what you mean. You should of course see the methods, because they are **declared** on `IOperations`. As to the usage: you could simply do `IOperations myOperations = new Operations();` or you could use one of the many dependency injection containers such as StructureMap and register the types like `.For.Use` which would be a starting point for dependency injection. Sorry I can't explain it more detailed in the comments, but it's a biiiig topic. – Thomas Flinkow Jun 04 '18 at 09:06
  • I edit my question to what i have done,i would appreciate if you take a look – mrslt Jun 04 '18 at 09:17
  • @mrslt well, unless you want to use a DI container then yes, what you have shown is the most of DI that you can currently do in this situation. – Thomas Flinkow Jun 05 '18 at 20:41