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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:22+00:00 2026-06-12T22:07:22+00:00

I have a scenario that I don’t know how best to resolve with Castle.

  • 0

I have a scenario that I don’t know how best to resolve with Castle. I am trying to resolve types based on an id that is retrieved from an external system. The tricky part is that multiple id’s relate to the same concrete type.

Here is what I have come up with so far. This just feels a bit wrong. I don’t like the fact that I needed to call ResolveAll.

    [TestFixture]
public class TestWindsor
{

    [Test]
    public void AddGreenResolverToContainer_ShouldResolveCorrectTypeAndTypeId()
    {
        //---------------Set up test pack-------------------
        IWindsorContainer container = new WindsorContainer();
        //---------------Assert Precondition----------------

        //---------------Execute Test ----------------------
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "1"}));
        //---------------Test Result -----------------------
        var actual = container.Resolve<IResolver>();
        Assert.IsInstanceOf<TestOneResolver>(actual);
        Assert.AreEqual("1", actual.typeId);
    }

    [Test]
    public void AddTwoNamedGreenResolverToContainer_ShouldResolveTwoTypes()
    {
        //---------------Set up test pack-------------------
        IWindsorContainer container = new WindsorContainer();
        //---------------Assert Precondition----------------

        //---------------Execute Test ----------------------
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "1"}).Named("tageventstatusresolver"));
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestOneResolver>()
                               .DependsOn(new {typeId = "2"}).Named("second"));            
        container.Register(Component.For<IResolver>()
                               .ImplementedBy<TestTwoResolver>()
                               .DependsOn(new {typeId = "1122"}).Named("redFirst"));
        container.Register(Component.For<IResolverFactory>()
                                    .ImplementedBy<ResolverFactory>()
                                    .DependsOn(new { resolvers=container.ResolveAll<IResolver>()}));


        //---------------Test Result -----------------------
        var actual = container.Resolve<IResolverFactory>();
        var resolver = actual.Create("1");
        Assert.IsInstanceOf<TestOneResolver>(resolver);
    }

}


public interface IResolverFactory
{
    IResolver Create(string typeId);
}

public class ResolverFactory : IResolverFactory
{
    private readonly IResolver[] _resolvers;

    public ResolverFactory(IResolver[] resolvers)
    {
        _resolvers = resolvers;
    }

    public IResolver Create(string typeId)
    {
        return _resolvers.Where(resolver => resolver.typeId == typeId).FirstOrDefault();
    }
}

public enum ResolutionStatus
{
    Red,
    Green,
    Amber
}

public interface IResolver
{
    string typeId { get; }
    ResolutionStatus Resolve();
}

public class TestOneResolver : IResolver
{
    public TestOneResolver(string typeId)
    {
        this.TypeId = typeId;
    }

    public string TypeId { get; private set; }

    public ResolutionStatus Resolve()
    {
        return ResolutionStatus.Green;
    }
}

public class TestTwoResolver : IResolver
{
    public TestTwoResolver(string typeId)
    {
        TypeId = typeId;
    }

    public string TypeId { get; private set; }

    public ResolutionStatus Resolve()
    {
        return ResolutionStatus.Red;
    }
}

Does anyone have suggestions as to a better way to do this?

  • 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-12T22:07:23+00:00Added an answer on June 12, 2026 at 10:07 pm

    Set component name as typeId value and follow this approach to have a typed factory creating by component name.

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

Sidebar

Related Questions

I have a scenario that i'd like to implement, and don't know where to
I have a scenario that my load balancer translates port 80 from outside into
I have a question about global.asax that I don't really understand. My scenario is
I have a fairly straight-forward scenario that I am trying to solve but I'm
I have a scenario where I have several classes that extend from a common
the scenario that I'm trying to accomplish is the following: I have the main
I have a path defined: when /the admin home\s?page/ /admin/ I have scenario that
I have a scenario that looks like this: #include <algorithm> using namespace std; //
I have a scenario that I haven't been able to solve: I'm toying around
I have a scenario that, I am creating dynamic html content and I need

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.