Microsoft Dependency Injection

ASP.NET Core recently hit the 1.0 milestone and one of the most interesting features in this version (or edition, I’m not sure of future of the MVC 5.X series) is that dependency injection is baked into the core of the framework.  That means that our controllers are no longer created by simplistic construction logic and can have complex dependencies.  In order to facilitate my unit testing requirements, my team has (for years) overridden the default IDependencyResolver in MVC to use Unity (Microsoft’s IoC library) to resolve all dependencies (“Services”) for MVC.  This approach is functional, and great for Unit Testing becuase you can inject mocked members into your controller, but it would sure be nice if that functionality was cooked into the framework so that my team didn’t have to bolt it on.  You can see the details about how to configure DI in MVC Core here.

Unfortunately, I am not ready to jump into MVC Core for my team’s production applications.  It’s not a me thing, it is a we thing.  Jumping into MVC Core is really a decision that all our development teams need to agree to and we just have too many other things to focus on.  I really believe that MVC core will be were we go in the future, so I did decide to just into the DI stack that Microsoft is using in MVC Core.  Unfortunately, Microsoft’s new DI stack is not Unity.  I decided to go against our defacto standard of Unity because if we do go to MVC core, there’s no reason to continue with Unity.  Unless the new DI stack is terribly complicated, this DI stack will become my team’s standard in the future, spoiler alert, IMHO this new DI stack is better and easier to use than Unity.

Unity (and Ninject, and AutoFac, etc) have a ton of awesomely cool features that I have never used in a professional project.  Since I understood the idea of Dependency Injection, I have desired a simpler tool.  In fact, to learn how DI works and create a simpler tool to use, I created my own IoC Container.  From my experience, and experimentation, the new DI stack is extremely simple.  The root interface, “IServiceProvider,” only has a single method, GetService.  There are several helper method to make it simpler to use, like the generic version of the method.

Dependency Injection Code