You are looking for the concept of Modules in autofac. For each level of your architecture, you add a new autofac module for that level, where you register the types of this layer. In ViewModelLocator , where you create your autofac container, you simply register autofac modules, rather than register all types directly.
In more detail, and for your case, it may look something like this:
In the Company.Services project:
You add a new ServicesModule module with something like this.
public class ServiceModule : Autofac.Module { protected override void Load(ContainerBuilder builder) {
In the project Company.ViewModels :
You also create a ViewModelModule where you register all of your ViewModels similar to the ServiceModule .
public class ViewModelModule : Autofac.Module { protected override void Load(ContainerBuilder builder) {
In the project Company.Wpf ( ViewModelLocator ):
var builder = new ContainerBuilder(); builder.RegisterModule<ViewModelModule>(); builder.Build();
Note that since we registered the ServiceModule within the ViewModelModule , we just need to register the ViewModelModule directly in the ContainerBuilder . This has the advantage that you do not need to add a link to the Company.Service project as part of the Company.Wpf project.
source share