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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:16:05+00:00 2026-05-30T22:16:05+00:00

Can Castle Windsor resolve a collection filtered by a string parameter? interface IViewFactory {

  • 0

Can Castle Windsor resolve a collection filtered by a string parameter?

interface IViewFactory
{
   IView[] GetAllViewsInRegion(string regionName);
}

My application defines regions as groups of IView-derived types. When I display a particular region at runtime, I want to resolve an instance of every IView type within it (a la Prism).

I’ve tried doing it with the Castle’s Typed Factory Facility, ComponentModel Construction Contributors, and Handler Selectors, but I can’t figure out how to map multiple types to a string in a way that Castle can access, nor how to extend Castle to check the string when it decides which types to try to resolve and return in the container.

  • 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-30T22:16:06+00:00Added an answer on May 30, 2026 at 10:16 pm

    Is selection by string strictly necessary? Would it be possible to instead have all IView implementations in the same “region” implement a dedicated interface that derives from IView? Then you could use WindsorContainer.ResolveAll() (passing your region-specific IView as T) to resolve the implementations for the region in question (or you could use one of the Collection Resolvers to perform constructor injection).

    In general, when trying to do things like this with Windsor, I make every effort to use the type system (and Windsor’s support thereof) before resorting to string-based solutions.

    Update: since we confirmed that selection by string is necessary in this case, the best solution I see is to simply inspect the list of handlers in the kernel that satisfy the IView service, then filter for the implementers where the region (defined via attribute) matches what we want, then resolve those implementers. This feels a bit hackish, but if you’re okay with having a direct reference to the container in your IViewFactory implementation, this appears to work fine. Below is a passing test case demonstrating the solution.

        [Test]
        public void Test()
        {
            using (var factory = new ViewFactory())
            {
    
                var regionOneViews = factory.GetAllViewsInRegion("One");
                Assert.That(regionOneViews, Is.Not.Null);
                Assert.That(regionOneViews, Has.Length.EqualTo(2));
                Assert.That(regionOneViews, Has.Some.TypeOf<RegionOneA>());
                Assert.That(regionOneViews, Has.Some.TypeOf<RegionOneB>());
    
                var regionTwoViews = factory.GetAllViewsInRegion("Two");
                Assert.That(regionTwoViews, Is.Not.Null);
                Assert.That(regionTwoViews, Has.Length.EqualTo(1));
                Assert.That(regionTwoViews, Has.Some.TypeOf<RegionTwoA>());
            }
        }
    }
    
    public interface IViewFactory
    {
        IView[] GetAllViewsInRegion(string regionName);
    }
    
    public class ViewFactory : IViewFactory, IDisposable
    {
        private readonly WindsorContainer _container;
    
        public ViewFactory()
        {
            _container = new WindsorContainer();
            _container.Register(
                Component.For<IView>().ImplementedBy<RegionOneA>(),
                Component.For<IView>().ImplementedBy<RegionOneB>(),
                Component.For<IView>().ImplementedBy<RegionTwoA>()
                );
        }
    
        public IView[] GetAllViewsInRegion(string regionName)
        {
            return _container.Kernel.GetHandlers(typeof (IView))
                .Where(h => IsInRegion(h.ComponentModel.Implementation, regionName))
                .Select(h => _container.Kernel.Resolve(h.ComponentModel.Name, typeof (IView)) as IView)
                .ToArray();
        }
    
        private bool IsInRegion(Type implementation,
                                string regionName)
        {
            var attr =
                implementation.GetCustomAttributes(typeof (RegionAttribute), false).SingleOrDefault() as RegionAttribute;
            return attr != null && attr.Name == regionName;
        }
    
        public void Dispose()
        {
            _container.Dispose();
        }
    }
    
    public interface IView {}
    
    [Region("One")]
    public class RegionOneA : IView {}
    
    [Region("One")]
    public class RegionOneB : IView {}
    
    [Region("Two")]
    public class RegionTwoA : IView {}
    
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class RegionAttribute : Attribute
    {
        private readonly string _name;
    
        public RegionAttribute(string name)
        {
            _name = name;
        }
    
        public string Name
        {
            get { return _name; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you get castle Windsor to choose the right implantation of a interface
Is registering components on castle windsor thread-safe? I.e., can multiple threads simultaneously register components
My current understanding of Castle Windsor registration is that one can only validate registration
Using Castle Windsor, I want to configure a generic service with a type parameter;
I know how to tell Castle Windsor to resolve a reference from a factory's
Possible Duplicate: Where can I download the Castle Windsor WcfIntegration Facilities dll? I need
I'm using Castle Windsor and Binsor to use dependency injection in my application. I'm
If you are using the LoggingFacility in Castle Windsor the container will automatically resolve
Using Castle.Windsor in ASP.NET MVC (3.0) is there any way I can appropriately handle
Can Castle Windsor do value injection? For example, say I have a model object

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.