5

I'm experimenting with IoC in my Web App and would like to do things according to best practices. Recently I discovered an IoC framework called DryIoc which is supposed to be small and fast.

I've read through the examples but none seem to point out where I should put the container itself.

Should it reside in the controller? Or in Global.asax? Someplace else maybe? Or perhaps as a static variable in a class?

I'd appreciate if someone would be able to guide me in the right direction, preferrably with some sample code, as I've stalled and don't got a clue on how to continue.

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

container.Register<ISalesAgentRepository, SalesAgentRepository>(Reuse.Singleton);
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • 2
    You need to use the container in the [composition root](http://blog.ploeh.dk/2011/07/28/CompositionRoot/). – Yacoub Massad Mar 21 '16 at 14:39
  • 1
    I would suggest using a more widely used container to get started with DI, as you will find much more support, examples, and tooling. If, after you have been using a different container, you decide you want to try these small, off-the-beaten-path containers, then you'll have the experience to know how to use them properly. – Erik Funkenbusch Mar 21 '16 at 14:50
  • 2
    Contrary to other comments and answers here, I'd recommend that you [don't use a DI container at all](http://blog.ploeh.dk/2014/06/10/pure-di). This will have the benefit that you'll learn the underlying patterns better, and you will not get stuck with some awkward API issue. – Mark Seemann Mar 21 '16 at 14:53
  • 2
    @MarkSeemann Really? Contrary to your comment, I'd recommend that we return all to assembler....................... – Matías Fidemraizer Mar 21 '16 at 14:58
  • 1
    Note that many IOC projects provide this support for you out of the box: https://www.nuget.org/packages/StructureMap.MVC5/ although if you use StructureMap4 you'll need to tweak 2 of the files that the MVC5 package drops in your solution https://github.com/webadvanced/Structuremap.MVC5/issues/15 – Chris Marisic Mar 21 '16 at 18:25

2 Answers2

2

Usually I do the following:

1 - Create a bootstrapper class

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 - Call the bootstrap method in the global.asax, in the Application_Start method:

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

And never use the container anywhere directly, you have to hook it somewhere in the MVC lifecycle, and usually the DI package you use can do this for you.

Also note that I've added a GetInstance<T> method to the bootstrapper-class. This method is what makes it possible to use the container directly by requesting instances of types. I've added this method so you know it is possible, but always use constructor-injection if possible.

Maarten
  • 22,527
  • 3
  • 47
  • 68
  • Maartn can you remove GetInstance method please :) Most of people who new di they will use it and they will resolve it from there. Beside of it you can put a method which is return new lifetimescope. – Erkan Demirel Mar 21 '16 at 15:32
  • 2
    Exactly how I do it, but I'll leverage WebActivator instead of polluting global asax any more than i'm forced to. – Chris Marisic Mar 21 '16 at 18:22
1

Actually, you may not need to store container on your side. Here is the DryIoc WebApi Owin sample.

The DryIoc.WebApi extension will store and Dispose the container when it is appropriate in IDependencyResolver implementation.

dadhi
  • 4,807
  • 19
  • 25