Skip to content

Instantly share code, notes, and snippets.

@tonyjoanes
Last active October 21, 2015 08:37
Show Gist options
  • Save tonyjoanes/0e564eb7e46f1907d711 to your computer and use it in GitHub Desktop.
Save tonyjoanes/0e564eb7e46f1907d711 to your computer and use it in GitHub Desktop.
Autofac convention based registration
private void BootAutoFac()
{
var builder = new ContainerBuilder();
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => x.FullName.StartsWith("Your.Namespace")).ToArray();
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.IsClass)
.AsImplementedInterfaces()
.InstancePerRequest();
// ect...
// ....
}
@tonyjoanes
Copy link
Author

Using autofac we can register all services and their implemented interfaces using a simple convention based approach. This saves the huge files of registration code that we typically see and also means you don't have to keep adding new interfaces and implementations to the registration code as long as the same pattern is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment