“The Unity Container (Unity) is a lightweight, extensible dependency injection container. It facilitates building loosely coupled applications”
This was very simple to setup for an ASP.NET MVC project (net461) using the nuget packages:
- Unity.Container, Version=5.7.0
- Unity.Abstractions, Version=3.3.0
- Unity.Mvc, Version=5.0.13
Implementation is as simple as this:
If you have worked with amazing Dependency Injection Frameworks such as Autofac or Ninject you would expect to be able to create cohesive modules right? Well Unity doesn’t support this out of the box however you can create extension methods that do pretty much the same thing, just remember the order will matter if you are going to be calling
container.Resolve later down the stack.
container.AddExtension and new up the module:
The module then inherits the
Container from the base class
Unity is unable to automatically resolve
IEnumerable<T> dependencies which you could use with an Abstract Factory.
You can however get around this by naming the dependency.
This is then automagically injected into the consumer.
public class FooController : Controller