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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:12:38+00:00 2026-05-20T12:12:38+00:00

With Castle Windsor, let’s say I have the following classes: public class LowLevelComponent {

  • 0

With Castle Windsor, let’s say I have the following classes:

public class LowLevelComponent
{
}

public class HighLevelComponent
{
    readonly LowLevelComponent LowLevelComponent;

    public HighLevelComponent(LowLevelComponent lowLevelComponent)
    {
        LowLevelComponent = lowLevelComponent;
    }
}

public class ComponentBeingResolved
{
    readonly LowLevelComponent LowLevelComponent;
    readonly HighLevelComponent HighLevelComponent;

    public ComponentBeingResolved(LowLevelComponent lowLevelComponent,
                                  HighLevelComponent highLevelComponent)
    {
        LowLevelComponent = lowLevelComponent;
        HighLevelComponent = highLevelComponent;
    }
}

Registered in the easiest possible way:

container.Register(Component.For<LowLevelComponent>());
container.Register(Component.For<HighLevelComponent>());
container.Register(Component.For<ComponentBeingResolved>());

I’d like to get the same instance of LowLevelComponent used in all dependencies each time I call Resolve.

So, after these calls:

var instance1 = container.Resolve<ComponentBeingResolved>();
var instance2 = container.Resolve<ComponentBeingResolved>();

The following assertions should be true:

instance1.LowLevelComponent == instance1.HighLevelComponent.LowLevelComponent
instance1.LowLevelComponent != instance2.LowLevelComponent
instance1.HighLevelComponent != instance2.HighLevelComponent

I’ll also take “you’re doing everything wrong, this is what you should do instead” as an answer 🙂

  • 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-20T12:12:39+00:00Added an answer on May 20, 2026 at 12:12 pm

    Based on Mauricio’s link, I got it working using factories:

    public interface IComponentFactory
    {
        T Get<T>();
    }
    
    var container = new WindsorContainer();
    container.AddFacility<TypedFactoryFacility>();
    container.Register(Component.For<LowLevelComponent>()
             .LifeStyle.Custom<ContextualLifestyle>());
    container.Register(Component.For<HighLevelComponent>()
             .LifeStyle.Custom<ContextualLifestyle>());
    container.Register(Component.For<IComponentFactory>().AsFactory());
    //Register the "context-root" component in a child container
    var subContainer = new WindsorContainer();
    subContainer.Register(Component.For<ComponentBeingResolved>()
                .LifeStyle.Transient);
    container.AddChildContainer(subContainer);
    container.Register(
        Component.For<ComponentBeingResolved>()
            .LifeStyle.Transient
            //Here's the magic
            .UsingFactoryMethod(
                () =>
                    {
                        using (new ContainerContext(container))
                            return subContainer.Resolve<ComponentBeingResolved>();
                    }));
    

    Usage:

    var factory = container.Resolve<IComponentFactory>();
    var instance1 = factory.Get<ComponentBeingResolved>();
    var instance2 = factory.Get<ComponentBeingResolved>();
    

    Not sure if this is a good hack or an ugly one, but it works wonderfully.

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

Sidebar

Related Questions

my global.asax looks like this Imports Castle.Windsor Imports Castle.Windsor.Configuration.Interpreters Public Class MvcApplication Inherits System.Web.HttpApplication
I have written the following simple test in trying to learn Castle Windsor's Fluent
Does Castle Windsor have an equivalent to Unity's BuildUp() whereby you can decorate class
Can Castle Windsor do value injection? For example, say I have a model object
I'm using Castle Windsor in my project. Some registered components are intercepted. Because the
When Instantiating a class, Windsor by default treats all public properties of the class
I have a basic working knowledge of Castle Windsor, but I cannot figure out
I have a console app using log4net (via Castle Windsor). Everything logs fine to
I have a reporting MVC application that uses Castle Windsor. On application start up
In Castle Windsor there is a feature called forwarded types where you can have

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.