title: Adding Autofac to WPF MVVM application
body: <p>I can't seem to find an solution to this problem. I've seen several questions about this, but none really give me a solution. I am totally new to Autofac and haven't really done much WPF + MVVM, but know the basics. </p>
  
  <p>I have a WPF application (using ModernUI for WPF) which I'm trying to add Autofac to, and I am having a hard time figuring out how to resolve my services within all the views, since they have no access to my container. I have a main view, which is my entry point, where I set up my container:</p>
  
  <pre><code>public partial class MainWindow : ModernWindow
  {
      IContainer AppContainer;
  
      public MainWindow()
      {
  
          SetUpContainer();
  
          this.DataContext = new MainWindowViewModel();
          InitializeComponent();
  
          Application.Current.MainWindow = this; 
      }
  
      private void SetUpContainer()
      {
          var builder = new ContainerBuilder();
  
          BuildupContainer(builder);
  
          var container = builder.Build();
  
          AppContainer = container;
      }
  
      private void BuildupContainer(ContainerBuilder builder)
      {
          builder.RegisterType&lt;Logger&gt;().As&lt;ILogger&gt;();
          ...
      }
  }
  </code></pre>
  
  <p>The problem I'm having is figuring out how I can resolve my logger and other services within my other views, where I inject all my dependencies through the ViewModel constructor, like so:</p>
  
  <pre><code>public partial class ItemsView : UserControl
  {
      private ItemsViewModel _vm;
  
      public ItemsView()
      {
          InitializeComponent();
  
          IFileHashHelper fileHashHelper = new MD5FileHashHelper();
          ILibraryLoader libraryLoader = new LibraryLoader(fileHashHelper);
          ILogger logger = new Logger();
  
          _vm = new ItemsViewModel(libraryLoader, logger);
          this.DataContext = _vm;
      }
  }
  </code></pre>
  
  <p>Some views have a ridiculous amount of injected parameters, and this is where I want Autofac to come in and help me clean things up. </p>
  
  <p>I was thinking of passing the container to the ViewModel and storing it as a property on my ViewModelBase class, but I've read that this would be an anti-pattern, and even then I don't know if that would automatically resolve my objects within the other ViewModels. </p>
  
  <p>I managed to put together a simple Console Application using Autofac</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
  
          var builder = new ContainerBuilder();
          builder.RegisterType&lt;Cleaner&gt;().As&lt;ICleaner&gt;();
          builder.RegisterType&lt;Repository&gt;().AsImplementedInterfaces().InstancePerLifetimeScope();
  
          var container = builder.Build();
  
          using (var scope = container.BeginLifetimeScope())
          {
  
              ICleaner cleaner = container.Resolve&lt;ICleaner&gt;();
              cleaner.Update(stream);
          }
      }
  }
  </code></pre>
  
  <p>but that was simple since it has a single entry point. </p>
  
  <p>I'd like some ideas on how to add Autofac to my WPF app. I'm sure that I'm doing something wrong. Your help is appreciated.</p>