Where to put the container?

I am experimenting with IoC in my web application and want to do things in accordance with best practices. I recently discovered an IoC structure called DryIoc, which should be small and fast.

I read the examples, but none of them indicate where I should put the container itself.

Should it be in the controller? Or in Global.asax? Where else could it be? Or perhaps like a static variable in a class?

I would appreciate it if someone could lead me in the right direction, preferably with some code example, since I got into a dead end and didn't understand how to proceed.

var container = new Container();   // Should obviously NOT be a local variable

container.Register<ISalesAgentRepository, SalesAgentRepository>(Reuse.Singleton);
+4
source share
2

:

1 - bootstrapper

public static class Bootstrapper {
    public static Container _container;
    public void Bootstrap() {
        var container = new Container;
        // TODO: Register all types
        _container = container;
    }
    public static T GetInstance<T>() {
        return _container.Resolve<T>();
    }
}

2. global.asax Application_Start:

protected void Application_Start() {
    Bootstrapper.Bootstrap();
}

, - MVC, DI .

, GetInstance<T> bootstrapper. , . , , , , -, .

+2

. DryIoc WebApi Owin.

DryIoc.WebApi , IDependencyResolver.

+1

All Articles