So, I'm trying to configure Autofac in my own ASP.Net web project. I modified existing ASP.Net projects that already have dependency injection installed, so I would like to know how to create a project from scratch. I asked a friend about how to do this, and he gave me the code.
This is absolutely fantastic because I know it will work. However, it sucks because - also with the help of a tutorial that I found on the Autofac website - it doesn’t explain much (I'm a lot more guy with pictures, I hate word problems. Keep that in mind!)
So, here is this code that works.
1 var builder = new ContainerBuilder();
2 builder.RegisterControllers(typeof(MvcApplication).Assembly);
3 builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
4 builder.RegisterModelBinderProvider();
5 builder.RegisterModule(new AutofacWebTypesModule());
6 builder.RegisterSource(new ViewRegistrationSource());
7 builder.RegisterType<foo>().As<ifoo>().InstancePerHttpRequest();
8 var container = builder.Build();
9 DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
- - - ? , .... / , /, . , .
:
1) , , ,
2) " -", . , FooController - BarController, BarController
3) - 6)
7) , 'ifoo' ; . , ;
8) . .
9) , . , - , , . , .