Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is AutoFac better than Microsoft.Extensions.DependencyInjection?

How is AutoFac better than Microsoft.Extensions.DependencyInjection? Autofac supports Property and Method injection (however I don't really understand where Property and Method injection might be useful).

I mean why should I use AutoFac when I can do the same thing by Microsoft.Extensions.DependencyInjection

like image 597
Sanket Sirotiya Avatar asked Aug 14 '20 06:08

Sanket Sirotiya


People also ask

Why should I use Autofac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

Should I use Autofac with ASP.NET core?

It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. Autofac is the most popular DI/IoC container for ASP.NET and it works with . NET Core flawlessly.

What is Autofac dependency injection?

¶ Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .

What is Autofac MVC?

Autofac is an IoC container that provides better integration for the ASP.NET MVC Framework, and manages dependencies between classes so that applications stay easy to change.


1 Answers

It's a fair question; fundamentally the difference is in the additional features Autofac can offer you if you need them. For simple applications, the Microsoft DI may offer enough functionality, but I've found as my application grows there's some extra features I find myself wanting/needing.

The features that in my mind encouraged me to originally start using Aufofac are:

  • Tagged lifetime scopes, and scoping services to those tags (https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#instance-per-matching-lifetime-scope)
  • Resolving a service with some associated Metadata (https://autofaccn.readthedocs.io/en/latest/advanced/metadata.html)
  • Defining Named/Keyed variants of a service (https://autofaccn.readthedocs.io/en/latest/advanced/keyed-services.html)
  • Resolving a factory function you can you use whenever you want (https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#dynamic-instantiation-func-b)
  • Lazy Instantiation (https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#delayed-instantiation-lazy-b)
  • The ability to combine all of these at your leisure (e.g. Meta<Lazy<IMyService>>)

There's plenty more, but those are some of my favourite features. Check the docs for all the goodness.

Also, there's no reason you can't start using the built-in DI and then add Autofac later when you realise you need it. The Autofac ASP.NET Core integration (https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html) will just pick up all your existing registrations, and then you can add those extra features on top.

In the interest of full disclosure here, I will point out that I am one of the maintainers of Autofac.

like image 156
Alistair Evans Avatar answered Sep 18 '22 23:09

Alistair Evans