Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use autofac in multiple project solution

I have large wpf application. I simplify my problem with autofac. Let say I have ViewModelLocator where I create contrainer. ViewModelLocator is in Company.WPF project, this project refers Company.ViewModels project.

var builder = new ContainerBuilder();
builder.RegisterType<MainWindowViewModel>().AsSelf().SingleInstance();
container = builder.Build();

Problem: MainWindowViewModel needs ICompanyService (I use CI) which is in Company.Services project, this project should not be reference from Company.WPF. ICompanyService is public and in same project (Company.Services) is also implementation CompanyService, but it is only internal.

How can I setup Autofac for these? I normally use castel Wisndor, there are installers for these situation, is similar option in Autofac too?

like image 552
Peter Pan Avatar asked Feb 07 '16 14:02

Peter Pan


People also ask

Why should I use Autofac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

What is Autofac dependency injection?

Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .


1 Answers

You are looking for the concept of Modules in autofac. For each layer in your architecture you add a new autofac module for that layer, where you register the types of that layer. In your ViewModelLocator, where you build your autofac container, you just register autofac modules instead of registering all types directly.

In more detail and for your case this could look something like this:

In your Company.Services project:

You add a new module ServicesModule with something like this. :

public class ServiceModule : Autofac.Module
{
  protected override void Load(ContainerBuilder builder)
  {
    // optional: chain ServiceModule with other modules for going deeper down in the architecture: 
    // builder.RegisterModule<DataModule>();

    builder.RegisterType<CompanyService>().As<ICompanyService>();
    // ... register more services for that layer
  }
}

In your Company.ViewModels project:

You also create a ViewModelModule where you register all your ViewModels similar to the ServiceModule.

public class ViewModelModule : Autofac.Module
{
  protected override void Load(ContainerBuilder builder)
  {
    // in your ViewModelModule we register the ServiceModule
    // because we are dependent on that module
    // and we do not want to register all modules in the container directly
    builder.RegisterModule<ServiceModule>();

    builder.RegisterType<MainViewModel>().AsSelf().InSingletonScope();
    // ... register more view models
  }
}

In your Company.Wpf project (ViewModelLocator):

var builder = new ContainerBuilder();
builder.RegisterModule<ViewModelModule>();
builder.Build();

Note that since we registered the ServiceModule within the ViewModelModule, we just have to register the ViewModelModule directly in the ContainerBuilder. This has the advantage of not needing to add a reference to the Company.Service project within the Company.Wpf project.

like image 105
M.E. Avatar answered Nov 06 '22 23:11

M.E.