0

I know ASP.NET MVC has DependencyResolver. How to have the similar application-wide access to IUnityContainer in non-MVC applications? Using public static class is nonsense. Here is my use case:

public partial class App : Application
{
    public App()
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterInstance(new MyClass());
    }
}
public class MainViewModel
{
    public MainViewModel()
    {
       IUnityContainer container = ???
       if (container.IsRegistered<MyClass>())
           DoSomething();
    }
}
Kok How Teh
  • 3,298
  • 6
  • 47
  • 85

2 Answers2

1

If you're looking for a solution that works for most applications you can register the component at the highest level, and then resolve it. As long as you resolve the instance Unity will resolve the dependencies (such as IUnityContainer).

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Registering dependencies ...");

        var container = new UnityContainer();
        container.RegisterType<ProgramStarter, ProgramStarter>();

        // Do other registrations.
        var program = container.Resolve<ProgramStarter>();

        // Since ProgramStarter was resolved using Unity it will also resolve the container.
        program.Run();
    }
}

public class ProgramStarter
{
    public ProgramStarter(IUnityContainer container)
    {
        // Do something with container.
    }

    public Run()
    {
        // Do stuff.
    }
}

Or an example for WPF:

IUnityContainer container = new UnityContainer();
// Do registrations.
var window = container.Resolve<MainWindow>();
window.Show();

MainWindow will now be able to resolve both the container and other dependencies.

Also, have a look at this question: Where to place and configure IoC container in a WPF application?

As a sidenote I usually keep my container as a static instance, and seen a lot of other implementations doing the same thing. I find it convenient to be able to use it when you find yourself in a situation when it's not possible to resolve it.

public static class IocContainer
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        // Possibly do registrations here as well...
        return container;
    });

    public static IUnityContainer Instance
    {
        get { return Container.Value; }
    }
}
Community
  • 1
  • 1
smoksnes
  • 10,509
  • 4
  • 49
  • 74
  • Thanks for your valuable insight. However, does this solution depend on having an interface as the parameter of the constructor? If yes, it will not work if the class is instantiated with a parameterless default constructor. For example, this solution does not work for ViewModel which needs a parameterless default constructor to be called from .xaml file. How to intuitively access IUnityContainer from the parameterless default constructor? – Kok How Teh Apr 12 '16 at 03:38
  • Yes, the solution is based upon using Unity for dependency injection, which will use interfaces in the constructor. I do not know any other way to fetch the container without setting it to a static variable as suggested in the answer. – smoksnes Apr 12 '16 at 05:10
  • I do not understand why the DependencyResolver cannot be generalized to projects other than ASP.NET... Any insight on this? – Kok How Teh Apr 12 '16 at 05:43
  • @KokHowTeh > Sorry, no insight regarding that. – smoksnes Apr 12 '16 at 05:47
0

I don't think there is any way other than you specified to implement IOC and DI for non MVC applications. Still if there's a way then any suggestions are appreciated.

Mohit
  • 522
  • 4
  • 10