Autofac register assembly types

Ivan-Mark Debono picture Ivan-Mark Debono · Nov 10, 2014 · Viewed 16.5k times · Source

In Castle, I used to do the following to register types from a different assembly:

Classes.FromAssemblyNamed("MyServer.DAL")
       .Where(type => type.Name.EndsWith("Repository"))
       .WithServiceAllInterfaces()
       .LifestylePerWebRequest(),

In Autofac, I change the above code to this:

builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
       .Where(t => t.Name.EndsWith("Repository"))
       .InstancePerRequest();

Is it correct?

Answer

Ivan-Mark Debono picture Ivan-Mark Debono · Nov 10, 2014

This is the correct way:

builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
       .Where(t => t.Name.EndsWith("Repository"))
       .AsImplementedInterfaces()
       .InstancePerRequest();