title: Is Module a binding context for Autofac
body: <p>I have an application which might needs to connect to multiple databases. But each module will only connect to one db. So I though it might make sense to isolate the db into each module so each module will get its own db auto resolved and I don't need to bother with named registration. But to my astonishment, it seems that Autofac's module is more a code module than a logical module (am I wrong here?): IA</p>
  
  <pre><code>[Test]
  public void test_module_can_act_as_scope_container()
  {
      ContainerBuilder builder = new ContainerBuilder();
      builder.RegisterModule(new Module1());
  
      IContainer c = builder.Build();
      var o = c.ResolveNamed&lt;CB&gt;("One");
      Assert.That(o.A.Name, Is.EqualTo("One"));
  
      builder = new ContainerBuilder();
      builder.RegisterModule(new Module1());
      builder.RegisterModule(new Module2());
      c = builder.Build();
      var t = c.ResolveNamed&lt;CB&gt;("One");
      Assert.That(t.A.Name, Is.EqualTo("Two"));
  }
  </code></pre>
  
  <p>And the interfaces/Modules used:</p>
  
  <pre><code>public interface IA
  {
      string Name { get; set; }
  }
  
  public class CA : IA
  {
      public string Name { get; set; }
  }
  
  public class CB
  {
      public CB(IA a)
      {
          A = a;
      }
      public IA A { get; private set; }
  }
  
  public class Module1 : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.Register(c =&gt; new CA() { Name = "One" }).As&lt;IA&gt;();
          builder.RegisterType&lt;CB&gt;().Named("One", typeof(CB));
      }
  }
  
  public class Module2 : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.Register(c =&gt; new CA() { Name = "Two" }).As&lt;IA&gt;();
          builder.RegisterType&lt;CB&gt;().Named("Two", typeof(CB));
      }
  }
  </code></pre>