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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:09:25+00:00 2026-06-06T09:09:25+00:00

I’m attempting to inject a dependency that varies by the state passed in. For

  • 0

I’m attempting to inject a dependency that varies by the state passed in. For example, if the state is Wisconsin, I want to inject one class, but if it’s Illinois, I want another. It’s not 1-for-1, but 7 states for one and 3 for another.

Is there a way in Spring.net to have a list of values to check against in the config xml?

  • 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-06T09:09:26+00:00Added an answer on June 6, 2026 at 9:09 am

    This is the subject of chapter 6.1 “Mapping runtime values to abstractions” of the book Dependency Injection in .NET. The solution suggested there is to use an Abstract Factory. Your abstract factory might look like:

    public interface IStateAlgorithmFactory
    {
        IStateAlgorithm Create(string state);
    }
    

    And inject this factory on your consumer that knows which state to process. To get an IStateAlgorithm his consumer then calls y

    alg = _factory.Create("Illnois");
    

    Optionally, you could create a simple factory that maps state names to instances managed by your spring container if you want full configuration control.

    Simple example

    I imagine you have several classes that implement a certain IStateAlgorithm:

    public interface IStateAlgorithm
    {
        string ProcessState(string stateName);
    }
    
    public class EchoingStateAlgorithm : IStateAlgorithm
    {
        public string ProcessState(string stateName)
        {
            return stateName;
        }
    }
    
    public class ReverseEchoingStateAlgorithm : IStateAlgorithm
    {
        public string ProcessState(string stateName)
        {
            return new string(stateName.Reverse().ToArray());
        }
    }
    

    And that there is a certain Consumer that needs to pick an algorithm based on a runtime value. The consumer can be injected with a factory, from which it can retrieve the algorithm it needs:

    public class Consumer
    {
        private readonly IStateAlgorithmFactory _factory;
    
        public Consumer(IStateAlgorithmFactory factory)
        {
            _factory = factory;
        }
    
        public string Process(string state)
        {
            var alg = _factory.Create(state);
            return alg.ProcessState(state);
        }
    }
    

    A simple factory implementation would simply switch on the state value, use an if, or look in internal list:

    public interface IStateAlgorithmFactory
    {
        IStateAlgorithm Create(string state);
    }
    
    public class StateAlgorithmFactory : IStateAlgorithmFactory
    {
        private string[] _reverseStates = new[] {"Wisconsin", "Alaska"};
    
        public IStateAlgorithm Create(string state)
        {
            if(_reverseStates.Contains(state))
                return new ReverseEchoingStateAlgorithm();
    
            return new EchoingStateAlgorithm();
        }
    }
    

    Spring.Net Configurable example

    If you would like to be able to configure your IStateAlgorithm in your spring configuration, you can introduce a LookupStateAlgorithmFactory. This example assumes that your IStateAlgorithms are stateless and can be shared among consumers:

    public class LookupStateAlgorithmFactory : IStateAlgorithmFactory
    {
        private readonly IDictionary<string, IStateAlgorithm> _stateToAlgorithmMap;
        private readonly IStateAlgorithm _defaultAlgorithm;
    
        public LookupStateAlgorithmFactory(IDictionary<string, IStateAlgorithm> stateToAlgorithmMap, 
                                            IStateAlgorithm defaultAlgorithm)
        {
            _stateToAlgorithmMap = stateToAlgorithmMap;
            _defaultAlgorithm = defaultAlgorithm;
        }
    
        public IStateAlgorithm Create(string state)
        {
            IStateAlgorithm alg;
            if (!_stateToAlgorithmMap.TryGetValue(state, out alg))
                alg = _defaultAlgorithm;
    
            return alg;
        }
    }
    

    The xml config could be:

    <object id="lookupFactory"
            type="LookupStateAlgorithmFactory, MyAssembly">
      <constructor-arg ref="echo" />
      <constructor-arg>
        <dictionary key-type="string" value-type="IStateAlgorithm, MyAssembly">
          <entry key="Alaska" value-ref="reverseEcho"/>
          <entry key="Wisconsin" value-ref="reverseEcho"/>
        </dictionary>
      </constructor-arg>
    </object>
    
    <object id="echo" type="EchoingStateAlgorithm, MyAssembly" />
    <object id="reverseEcho" type="ReverseEchoingStateAlgorithm, MyAssembly" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.