Skip to content

Instantly share code, notes, and snippets.

@techniq
Last active December 20, 2015 00:28
Show Gist options
  • Select an option

  • Save techniq/6041563 to your computer and use it in GitHub Desktop.

Select an option

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
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