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 9028157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:55:05+00:00 2026-06-16T06:55:05+00:00

I have an application that uses several different database connections. I would like to

  • 0

I have an application that uses several different database connections. I would like to use Castle Windsor’s typed factory support to inject a factory that returns instances of IDbConnection into my repositories and other components.

Here’s how the interface for my typed factory looks like:

public interface IConnectionFactory
{
    IDbConnection CreateConnection();
}

And here’s a sample repository:

public class AccountRepository
{
     private readonly IConnectionFactory connectionFactory;

     public AccountRepository(IConnectionFactory connectionFactory)
     {
         this.connectionFactory = connectionFactory;
     }

     public Account FindAccountById(int id)
     {
         using (var connection = connectionFactory.CreateConnection())
         {
             // TODO use the connection here
         }
     }
}

And an excerpt of my component registration:

// ...

container.Register(Component.For<IDbConnection>()
    .ImplementedBy<NpgsqlConnection>()
    .LifestyleTransient());

container.Register(Component.For<IConnectionFactory>()
     .AsFactory());

// ...

All my IDbConnections are implemented using the same type (NpgsqlConnection), but I have several different connection strings I need to use. Ideally, I would like to setup Windsor to set the connection string on the IDbConnection (or pass it into the connection constructor) for me depending on the component that has IConnectionFactory as a dependency.

For example:

  • Say I have two repositories, AccountRepository and ItemRepository, that take IConnectionFactory as a dependency in their constructors.
  • Say I also have two connection strings named foo and bar.
  • When AccountRepository calls IConnectionFactory.CreateConnection(), I want it to return an IDbConnection using connection string foo.
  • However, when a ItemRepository calls IConnectionFactory.CreateConnection(), I want it to return an IDbConnection using connection string bar.

Is this possible with Castle Windsor?

I’ve researched this topic but haven’t find any solution that fits my exact scenario. Ideally, I would like to keep my repositories ignorant of the exact connection string to use, i.e. I do not want to change my connection factory to:

public interface IConnectionFactory
{
    IDbConnection CreateConnection(string connectionStringName);
}

I’ve also read all the documentation and articles I could find on ISubDependencyResolver and ITypedFactoryComponentSelector but I’m not sure if they are applicable in this scenario or how to piece everything together.

By the way, I am using Castle Windsor 3.1 and .NET 4.0.

  • 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-06-16T06:55:06+00:00Added an answer on June 16, 2026 at 6:55 am

    Maybe use a real Factory, and not a typed one:

    public ConnectionAFactory : IConnectionFactory
    {
         public ConnectionAFactory()
         {
              this.connectionString = "foo";
         }
    }
    

    And register it in windsor with the correct Repository:

    Component.For<AccountRepository>()
        .DependsOn(
            ServiceOverride.ForKey<IConnectionFactory>().Eq(typeof(ConnectionAFactory)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses several different Java classes and would like to
I am building an android application that uses several third party libraries. I have
I have an application that uses AJAX liberally. I have several places where a
I have an application that uses rest to communicate to a server, i would
We have an application that uses Hibernate's 2nd level caching to avoid database hits.
We have an application that uses several out of process COM objects for various
I have a REST application that uses RESTlets. I am querying several databases to
I have a Pyramid app that uses Chameleon for its templates. I would like
I have a C# WPF application that use several library assemblies. I was wondring
I have a C# application that uses several web services which were added to

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.