55

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

Sanket Sirotiya
  • 576
  • 1
  • 5
  • 6
  • Related: https://stackoverflow.com/questions/30681477/why-would-one-use-a-third-party-di-container-over-the-built-in-asp-net-core-di-c – Steven Aug 14 '20 at 19:43

1 Answers1

67

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:

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.

hIpPy
  • 4,649
  • 6
  • 51
  • 65
Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
  • 1
    You forgot to mention IMO the most important feature: decorators. – Steven Aug 14 '20 at 19:42
  • 2
    Yeah, thats another great one; if I listed all the features I could be here all day! :) – Alistair Evans Aug 14 '20 at 20:56
  • I've been using autofac for years, it is completely rock solid and does absoutely everything I want it to do. Microsoft DI seems ok (it's not Prism!) but being able to resolve factories when you want to create something in the class is such a key idiom, which is what autofac excels at that I'm not switching. – satnhak Dec 05 '20 at 18:48
  • 1
    I am using Unity for many years and it id dead now, so I am looking for a replacement. Autofac seems to be my favorite. I was also looking at SimpleInjector but I hate it's lock after first resolve feature. – Karel Kral Jan 13 '22 at 12:21
  • i personally use autofac due to its property auto wireup feature Also wanted to know if anyone knows about performance diff – Uttam Ughareja Apr 16 '22 at 07:34
  • You forgot to mention that MS DI works with web forms, and Autofac doesn’t. – jmoreno May 06 '22 at 13:11
  • Main "not buying" point for me was that this will compile `b.RegisterType().As()`, where Microsoft ServiceCollection will fail ;) – Fabio Jun 01 '22 at 20:56
  • @jmoreno what do you mean ? Autofac supports webforms. https://docs.autofac.org/en/stable/integration/webforms.html – AFract Dec 21 '22 at 08:12
  • @AFract: it doesn't support constructor injection in webforms – jmoreno Dec 21 '22 at 14:20