Last active
December 20, 2015 00:28
-
-
Save techniq/6041563 to your computer and use it in GitHub Desktop.
NPoco with Autofac and ASP.NET MVC https://code.google.com/p/autofac/wiki/MvcIntegration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MvcApplication : HttpApplication | |
| { | |
| protected void Application_Start() | |
| { | |
| AreaRegistration.RegisterAllAreas(); | |
| RegisterRoutes(RouteTable.Routes); | |
| SetupIoc(); | |
| } | |
| private void SetupIoc() | |
| { | |
| var builder = new ContainerBuilder(); | |
| builder.Register(c => new Database("CONNECTION_STRING_KEY")) | |
| .As<IDatabase>() | |
| .InstancePerHttpRequest(); | |
| builder.RegisterModule(new AutofacWebTypesModule()); | |
| builder.RegisterSource(new ViewRegistrationSource()); | |
| builder.RegisterType<ExtensibleActionInvoker>() | |
| .As<IActionInvoker>(); | |
| builder.RegisterControllers(typeof(MvcApplication).Assembly) | |
| .InjectActionInvoker() | |
| .PropertiesAutowired() | |
| .OnActivated(e => | |
| { | |
| dynamic viewBag = ((Controller)e.Instance).ViewBag; | |
| viewBag.SiteTitle = "Example Site"; | |
| }); | |
| var container = builder.Build(); | |
| DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment