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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:10:57+00:00 2026-05-25T13:10:57+00:00

I have 2 components (AWriter and BWriter) where both implement IWriter interface. And I

  • 0

I have 2 components (AWriter and BWriter) where both implement IWriter interface. And I have 2 components (AComponent and BComponent) where both implement interface IComponent and both have dependency on IWriter. But in AComponent I want to tell Windsor to give me instance of AWriter, and in BComponent I want to tell Windsor to give me instance of BWriter. How to do that?

Here is the code:

public interface IWriter
    {
        void Write();
    }

    public class AWriter : IWriter
    {
        public void Write()
        {
            Console.Write("A writer");
        }
    }

    public class BWriter : IWriter
    {
        public void Write()
        {
            Console.Write("B writer");
        }
    }

    public interface IComponent
    {
        void Do();
    }

    public class AComponent : IComponent
    {
        IWriter writer;

        public AComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

    public class BComponent : IComponent
    {
        IWriter writer;

        public BComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

NOTE: This is just simplified problem and putting AWriter in AComponent and BWriter in BComponent as dependency is not an option. Also AComponent and BComponent might look same at this example, but this is not real situation. I have much more dependencies and components in real situation and I didn’t wanted to bother with that.

So, is there a way to somehow specify by some attribute which implementation should Castle give me?

  • 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-25T13:10:58+00:00Added an answer on May 25, 2026 at 1:10 pm

    You want to be using named components and service overrides. See here in Windsor’s documentation for help: http://docs.castleproject.org/Windsor.Registering-components-one-by-one.ashx

    Using fluent registration you’d do something like:

    container.Register(Component.For<IComponent>()
                                .ImplementedBy<AWriter>().Named("AWriter"),
                       Component.For<IComponent>()
                                .ImplementedBy<BWriter>().Named("BWriter"));
    

    Then:

    container.Register(Component.For<IComponent>()
                                .ImplementedBy<AComponent>()
                                .ServiceOverrides(
                                    ServiceOverride.ForKey("writer").Eq("AWriter"));
    container.Register(Component.For<IComponent>()
                                .ImplementedBy<BComponent>()
                                .ServiceOverrides(
                                    ServiceOverride.ForKey("writer").Eq("BWriter"));
    

    Of course this will result in two registrations for IComponent, but again if you need to inject the specific instances you can name them and use service overrides to inject each implementation into its appropriate spot in your dependency structure.

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

Sidebar

Related Questions

Delphi components have CreateWnd and CreateWindowHandle (and DestroyWnd and DestroyWindowHandle ). They're both intended
I have components in a View and adding them in a ScrolledForm . But
We have two components: enterprise application X, and Web service Y We want to
I have an annoying dependency problem between components, and i would like to hear
I know you can have multiple components in a picker view but is there
Upgrades work fine if no components have changed, but any time a component changes
I have two components that that communicate via TCP/IP. Component A acts as a
I have two components. One is called InsideComp and one is called OutsideComp. OutsideComp
We have few components like libraries dlls When initially created I ran the following
My situation: I have several components, which sometimes have changes to them, and are

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.