Autofac - Key Explanations for Configuring Dependency Injection

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.

// Create your builder.
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());

// assign interfaces here to the container so that it knows how to resolve requests
7  builder.RegisterType<foo>().As<ifoo>().InstancePerHttpRequest();

// create container
8 var container = builder.Build();
9 DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

- - - ? , .... / , /, . , .

:

1) , , ,

2) " -", . , FooController - BarController, BarController

3) - 6)

7) , 'ifoo' ; . , ;

8) . .

9) , . , - , , . , .

+4
1

3) RegisterControllers - . - , IModelBinder. , , , , . : http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder

4) RegisterModelBinderProvider AutofacModelBinderProvider - , RegisterModelBinders, .

5) RegisterModule autofac - , . Load, . AutofacWebTypesModule : https://code.google.com/p/autofac/source/browse/src/Source/Autofac.Integration.Web.Mvc3/AutofacWebTypesModule.cs?r=94f70ab10f4d65991c600e2e80171ce4847589e6 , HttpContext.Current. HttpContext , , , . AutofacWebTypesModule .

6) RegisterSource . . , autofac (, ISomeInterface, Owned, ). : http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/. ViewRegistrationSource : https://code.google.com/p/autofac/source/browse/src/Source/Autofac.Integration.Mvc/ViewRegistrationSource.cs?r=8974cef867b5b2f2876a8c71037014424bd87aba , , WebViewPage, ViewPage, ViewMasterPage ViewUserControl. cshtml, WebViewPage - - cshtml . RazorGenerator WebViewPage cshtml . ViewRegistrationSource, WebViewPage HomeView.cs, autofac context.Resolve() , .

: https://code.google.com/p/autofac/source/browse/Mvc3Integration.wiki?repo=wiki&r=7393a4178bea525f783cef91e206fbdc3921411a

+2

All Articles