Is it possible to get the current Unity container inside the controller

I registered the container unit as follows:

var container = new UnityContainer(); container.RegisterType<ICacheManager, CacheManager>(new ContainerControlledLifetimeManager()) 

Is it possible to access this "container" from the controller

+6
source share
4 answers

One way to do this, which I often do for convenience, is to declare your container as a global variable in the Global.ascx.cs file, for example:

 public class MvcApplication : System.Web.HttpApplication { public static UnityContainer Container; protected void Application_Start() { // assuming your initialize here } } 

However, this is pretty hack-ish.

The right thing is to use Unity to resolve your controllers ( See this article about creating a factory unity controller ) and then allow the unit to inject any dependencies into your controller when it decides the controller.

So a controller like:

 public MyController: Controller { public ICacheManager CacheManager {get;set;} } 

Automatically recognizes any dependencies that your container has registered.

+6
source

Not sure why you need this, but here is the code:

 public ActionResult Area51() { var _service = DependencyResolver.Current.GetService(typeof (IDummyService)); return View(); } 

If you are trying to inject a controller, you must set DepedencyResolver to use your IoC container to run the Global.asax application. Then MVC will automatically inject the dependency with your controller.

 var container = new UnityContainer(); DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container)); 

See another example:

http://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-using-Unity-IoC-Container.html

+8
source

I assume that you are authorizing the controller instance using the container. In this case, you can get the IUnityContainer controller as a dependency, like any other.

What are you trying to achieve? Getting a container in permitted classes is not big, because it connects your classes to the container and can usually be replaced with other mechanisms.

 class Program { static void Main(string[] args) { var container = new UnityContainer(); var foo = container.Resolve<MyController>(); } } public class MyController { private IUnityContainer container; public MyController(IUnityContainer container) { this.container = container; } } 
+6
source

While this is possible, it is best to avoid this.

It is best that you fulfill any dependencies required by the controller through the constructor parameters. Thus, the class (controller) makes it clear what are the dependencies necessary to run it.

When properly configured, the container will provide these dependencies and their dependencies (if any), etc.

The use of an IoC container should usually be limited to only one place within the application (called the root of the composition). Any additional link / call to the container is susceptible to the service locator antivirus . See the related article for reasons why this approach may be bad.

+3
source

All Articles