Robin Sun
Robin Sun

Reputation: 1620

DependencyResolver.SetResolver(new AutofacDependencyResolver(container)) Advantage

DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

The function of the above code is to let the Container to manage the Controller instances. When a web request comes to the server, the server will get a Controller instance from the Container to handler the request.

But without Container, the server will automaticlly create a Controller instance.

What is the advantage of the management of Controller in Container? Thank you.

Upvotes: 1

Views: 1156

Answers (1)

agartee
agartee

Reputation: 445

The advantage is that you can store all of your IoC type-registrations in a single container, and therefore manage their dependencies cleanly with the IoC container of your choice (Autofac, Windsor, Unity, Ninject, etc.). If your controllers have dependencies, this lets you avoid service-locating them within its methods.

I gave an usage example here for Unity. I am using Autofac for my own project but following the same pattern.

Upvotes: 1

Related Questions