0

I am beginner in dependency injection.I used Microsoft Unity in console application to start with and to brush up basics. All I wanted to call method of Addition Class or Substraction Class based on the class I register to interface reference. But I am getting refernce InterfaceReference as null. Please suggest where I am doing wrong in below case ?

I used one interfacer -

namespace ConsoleApplication3
{
    interface IArithmetic
    {
        int DoOperation(int a, int b);
    }
}

And 2 claess -

namespace ConsoleApplication3
{
    class Addition : IArithmetic
    {
        public int DoOperation(int a, int b)
        {
            return a + b;
        }
    }
}

namespace ConsoleApplication3
{
    class Substraction : IArithmetic
    {
        public int DoOperation(int a, int b)
        {
            return a - b;
        }
    }
}



namespace ConsoleApplication3
{
    class Program
    {
        private static IArithmetic InterfaceReference;

        public Program(IArithmetic InterfaceReferenceParam)
        {
            InterfaceReference = InterfaceReferenceParam;
        }

        static void Main(string[] args)
        {

            IUnityContainer myContainer = new UnityContainer();
            myContainer.RegisterType<IArithmetic, Addition>();
            int output = InterfaceReference.DoOperation(5, 2);
            Console.Write(output);
            Console.WriteLine();
        }
    }
}
Nitendra Jain
  • 489
  • 1
  • 7
  • 24

2 Answers2

1

You're not instantiating your Program class, so the constructor that injects the IArithmetic implementation is never run. You need to use Unity Resolve to create the instance of the Program class. Take a look at Using Unity DI with a Console Application

Something like the following code (I haven't used unity for a while, so I'm not sure if the syntax is totally correct). Basically you also need to register the Program class with Unity, and then use Unity Resolve method to create an instance of Program. Because Unity knows about Program, this resolve will create the dependency tree for Program.

namespace ConsoleApplication3
{
    class Program
    {
        private IArithmetic InterfaceReference;

        public Program(IArithmetic InterfaceReferenceParam)
        {
            InterfaceReference = InterfaceReferenceParam;
        }

        public void Run()
        {
            int output = InterfaceReference.DoOperation(5, 2);
            Console.Write(output);
            Console.WriteLine();

        }

        static void Main(string[] args)
        {
            IUnityContainer myContainer = new UnityContainer();
            myContainer.RegisterType<IArithmetic, Addition>();
            myContainer.RegisterType<Program, Program>();
            var program = myContainer.Resolve<Program>();
            program.Run();
        }
    }
}
Community
  • 1
  • 1
Nibor
  • 1,096
  • 8
  • 11
  • thanx it worked for me. However, how much necessary is this - myContainer.RegisterType(); because code works without this also. – Nitendra Jain Feb 14 '17 at 10:34
  • Leave it out then! It's a while since I used Unity and I wasn't sure if you had to explicitly register Program with Unity before using Resolve. Often the entry point will implement an interface and you will register the implementation of the interface, then call resolve on the interface – Nibor Feb 14 '17 at 10:40
0

The issue is that you are not calling the Unity Resolve method after registering the type. This, for instance, will work:

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType(typeof(IArithmetic), typeof(Addition));
        var arithmeticHelper = container.Resolve<IArithmetic>();
        int output = arithmeticHelper.DoOperation(5, 2);
        Console.Write(output);
        Console.ReadKey();
    }
}
cookiemonster
  • 131
  • 1
  • 5