C# Dependency Injection with Autofac
Why Stack Overflow recommends it
Stack Overflow users link it when they are learning dependency injection and Autofac and want a walkthrough they can follow along with.
Recommended by 1 people across 1 different months, 2019 to 2020.
What it is
A 55-minute tutorial by Tim Corey on dependency injection in C# using the free Autofac container. He takes a small sample app, applies the Dependency Inversion Principle, then wires up Autofac and shows why DI helps with unit testing and changing code.
What people said
Recommends sephiroth · 2019-10 · Stack Overflow · on “how to pass generic interface in constructor of a class with type of that class with autofac”
You need to register your components for the Dependency Injection. That's done by creating a ContainerBuilder and registering your components. // create the ContainerBuilder var builder = new ContainerBuilder(); // register your components builder.RegisterType<MyConcreteType1>().As<IMyInterface1>(); builder.RegisterInstance(new MyConcreteType2()).As<IMyInterface2>(); ... // build your container var myContainer = builder.Build(); And then to get your required component all you have to do is // create a scope so AutoFac will take care of // disposing of objects you no longer need using (var scope = myContainer.BeginLifetimeScope()) { // resolve the components you need. AutoFac will take care o
Cites Alejandro H · 2019-12 · Stack Overflow · on “NullReferenceException when trying to add a new object to a list using inheritance”
I just watched this video, [this video] , and I dont think I have a dependency injector. In the video he uses autofac, looks like there is many to chose from, not sure if that matters.
Cites Jseb · 2020-05 · Stack Overflow · on “AutofacWebApiDependencyResolver method doesnt exist”
I am following this tutorial [this video] for dependency injection. This clear thing out for me however it does not show how to implement on a Web API using n-tier architecture. I also included followed this tutorial: C-Sharp AutoFac Web API Tutorial [LINK] . This is what my code look like: using Autofac; using System.Linq; using System.Reflection; using System.Web.Http; namespace WebAPI.App_Start { public class AutofacWebapiConfig { public static IContainer Container; public static void Initialize(HttpConfiguration config) { Initialize(config, RegisterServices(new ContainerBuilder())); } public static void Initialize(HttpConfiguration config, IContainer container) { config.DependencyResolver = n