Search code examples
unity-container

Can we request unity to create an instance that does not implement interface?


I have public class i.e. public class Student and i am currently instantiating it using new i.e. Student st = new Student() keyword.

This class does not implement any interface.

Can i request Microsoft Unity to create an instance of this class?

I only know we can request IOC if that class implement interface.


Solution

  • yes, you can for example resolve it directly:

    var container = new UnityContainer();
    var s = container.Resolve(typeof(Student));
    

    or you can register an Instance:

    container.RegisterInstance(s);
    

    which will be used if another class needs a Student instance in the constructor.