Last active
August 29, 2015 14:16
-
-
Save danielmackay/e5362b7c319acd3cec93 to your computer and use it in GitHub Desktop.
MVC Autofac
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 DataModule : Autofac.Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
var asm = Assembly.GetExecutingAssembly(); | |
builder.RegisterAssemblyTypes(asm) | |
.Where(t => t.Name.EndsWith("Repository")) | |
.AsImplementedInterfaces() | |
.InstancePerRequest(); | |
builder.RegisterType<PbcContext>() | |
.AsSelf() | |
.InstancePerRequest(); | |
} | |
} |
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 static class IocConfig | |
{ | |
public static void RegisterDependencies() | |
{ | |
// Standard setup | |
var builder = new ContainerBuilder(); | |
ConfigureConventions(builder); | |
RegisterControllers(builder); | |
RegisterModules(builder); | |
//RegisterModelBinders(builder); | |
RegisterHttp(builder); | |
// Load the container | |
InitContainer(builder); | |
} | |
private static void ConfigureConventions(ContainerBuilder builder) | |
{ | |
// placed here before RegisterControllers as last one wins | |
builder.RegisterAssemblyTypes() | |
.Where(t => t.Name.EndsWith("Repository")) | |
.AsImplementedInterfaces() | |
.InstancePerRequest(); | |
builder.RegisterAssemblyTypes() | |
.Where(t => t.Name.EndsWith("Service")) | |
.AsImplementedInterfaces() | |
.InstancePerRequest(); | |
} | |
/// <summary> | |
/// Register all controllers for the assembly | |
/// </summary> | |
private static void RegisterControllers(ContainerBuilder builder) | |
{ | |
// Note that ASP.NET MVC requests controllers by their concrete types, | |
// so registering them As<IController>() is incorrect. | |
// Also, if you register controllers manually and choose to specify | |
// lifetimes, you must register them as InstancePerDependency() or | |
// InstancePerHttpRequest() - ASP.NET MVC will throw an exception if | |
// you try to reuse a controller instance for multiple requests. | |
builder.RegisterControllers(typeof(MvcApplication).Assembly); | |
} | |
private static void RegisterModules(ContainerBuilder builder) | |
{ | |
// Picks up modules found in other assembly we are referencing | |
var asm = AppDomain.CurrentDomain.GetAssemblies(); | |
builder.RegisterAssemblyModules(asm); | |
} | |
/// <summary> | |
/// Model binder providers - excluded - not sure if need | |
/// </summary> | |
private static void RegisterModelBinders(ContainerBuilder builder) | |
{ | |
builder.RegisterModelBinders(Assembly.GetExecutingAssembly()); | |
builder.RegisterModelBinderProvider(); | |
} | |
private static void RegisterHttp(ContainerBuilder builder) | |
{ | |
/* | |
The MVC Integration includes an Autofac module that will add HTTP request | |
lifetime scoped registrations for the HTTP abstraction classes. The | |
following abstract classes are included: | |
-- HttpContextBase | |
-- HttpRequestBase | |
-- HttpResponseBase | |
-- HttpServerUtilityBase | |
-- HttpSessionStateBase | |
-- HttpApplicationStateBase | |
-- HttpBrowserCapabilitiesBase | |
-- HttpCachePolicyBase | |
-- VirtualPathProvider | |
To use these abstractions add the AutofacWebTypesModule to the container | |
using the standard RegisterModule method. | |
*/ | |
builder.RegisterModule<AutofacWebTypesModule>(); | |
} | |
private static void InitContainer(ContainerBuilder builder) | |
{ | |
// Set the MVC dependency resolver to use Autofac | |
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