Last active
October 21, 2015 08:37
-
-
Save tonyjoanes/0e564eb7e46f1907d711 to your computer and use it in GitHub Desktop.
Autofac convention based registration
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
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... | |
// .... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.