Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7401275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:25:23+00:00 2026-05-29T04:25:23+00:00

I have an application which might needs to connect to multiple databases. But each

  • 0

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

[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<CB>("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<CB>("One");
    Assert.That(t.A.Name, Is.EqualTo("Two"));
}

And the interfaces/Modules used:

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 => new CA() { Name = "One" }).As<IA>();
        builder.RegisterType<CB>().Named("One", typeof(CB));
    }
}

public class Module2 : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c => new CA() { Name = "Two" }).As<IA>();
        builder.RegisterType<CB>().Named("Two", typeof(CB));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:25:25+00:00Added an answer on May 29, 2026 at 4:25 am

    Yes, you’re kind of correct.

    Modules serve only for splitting configuration into somewhat independent parts. They do not scope configuration in any way. Having modules is actually the same as if you merged all modules’ Load methods’ code into a single configuration method and then built the container.

    In your case your Module2 actually overrides the registration for IA interface from Module1.

    I’ve also been interested in finding a solution to the problem. I’ve come to the following approach:

    Keyed service

    var key = new object();
    builder.Register(c => new CA() { Name = "Two" }).Keyed<IA>(key);
    builder.RegisterType<CB>().Named("Two", typeof(CB))
      .WithParameter(new ResolvedParameter(
          (pi, ctx) => pi.Type == typeof(IA),
          (pi, ctx) => ctx.ResolveKeyed<IA>(key)
      ));
    

    Pros:

    You can control which IA instances will be injected in each module.

    Contras:

    1. It’s quite a lot of code
    2. It does not make IA component ‘internal’ to the module – other modules can still resolve it using simple Resolve<IA>. Modules aren’t isolated.

    Hope this helps

    UPDATE

    In some cases it may be easier, and frankly more correct from design point of view, to make it this way:

    Delegate registration

    builder.Register(ctx => new CB(new CA { Name = "Two" }))
      .Named("Two", typeof(CB));
    

    Pros:

    1. You don’t expose your module-specific CA to other modules

    Contras:

    1. If CA and CB have complex dependencies and a lot of constructor parameters, you’ll end up with mess of constructing code
    2. If you need to use CA in several places inside a module, you’ll have to find a way to avoid copy-pasting

    Nested Container instances

    And yet another option is having an independent Container inside each module. This way all modules will be able to have their private container configurations. However, AFAIK, Autofac doesn’t provide any built-in means to somehow link several Container instances. Although I suppose implementing this should not be very difficult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application which needs to use a dll (also written by me) which
I have an application which really should be installed, but does work fine when
We have an application which needs to use Direct3D. Specifically, it needs at least
I have one application which uses the standard .NET forms authentication cookie, now I
I have an application which needs to run several other applications in chain. I
In my web application I have a part which needs to continuously crawl the
I have a WPF application which uses some library code for authentication which needs
I have an unattended touch screen kiosk application which needs to be able to
I have a Rails 3 web application which needs to: retrieve a stream data
I have an application which extracts data from an XML file using XPath. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.