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

  • Home
  • SEARCH
  • 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 5971653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:32:46+00:00 2026-05-22T20:32:46+00:00

I have a code similar to this: public static IEnumerable<T> ParseInput<T>(string input) { var

  • 0

I have a code similar to this:

    public static IEnumerable<T> ParseInput<T>(string input)
    {
        var xml = XElement.Parse(input);
// some more code here
        var parser = Container.Current.Resolve<IParser<T>>();
        return parser.Parse(xml);
    }

It contains some common processing of the data followed by a call to parser interface which is very different between the specializations. Currently, I am resolving the parser interface from a container – but I’m a bit uncomfortable with using a container from a static method.

Is there a better or alternative way to resolve the interface, besides this and the switch statement?

Edit: In my opinion, the shagginess of this comes from an attempt to marry unit-testable OO programming (IoC, no static classes) to functional programming. Where is the cleanest line of separation? Probably resolving the parser earlier and passing it into the static method.

  • 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-22T20:32:47+00:00Added an answer on May 22, 2026 at 8:32 pm

    Edit: I kinda missed what you meant. I quite like using a container for things like that. Switch statements are a bit ugly. So going with a container, some ways you could make it more testable are:

    If some code here is complicated, I tend to make sure it is not static, as its not as easy to unit test static methods when using dependency injection. If its not complicated, your code is fine to me :o)

    Option 1: Don’t use a static method.

    Instead use an instance method and inject the parser into it.

    public static IEnumerable<T> ParseInput<T>(string input)
    {
        var parser = Container.Current.Resolve<IXmlParser<T>>();
        return parser.ParseInput(input);
    }
    
    public class XmlParser : IXmlParser<T>
    {
      private readonly IParser<T> _parser;
      public XmlParser(IParser<T> parser)
      {
         _parser= parser;
      }
      public IEnumerable<T> ParseInput(string input)
      {
          var xml = XElement.Parse(input);
          // some more code here
          return _parser.Parse(xml);
      }
    }
    

    Option 2: Pass in the container resolver along with the string input. Although if doing this, option 3 is better.

    public static IEnumerable<T> ParseInput<T>(string input, IObjectResolver resolver)
    {
        var xml = XElement.Parse(input);
        // some more code here
        var parser = resolver.Resolve<IParser<T>>();
        return parser.Parse(xml);
    }
    

    Option 3: Pass in the already resolved parser (credit to Mark Seemann for this idea)

    public static IEnumerable<T> ParseInput<T>(string input, IParser<T> parser)
    {
        var xml = XElement.Parse(input);
        // some more code here
        return parser.Parse(xml);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this in my application: class A { public: int
I have code similar to this filtering entries in an Array of Objects: var
How do you make SMO release it's connections? I have this code: public static
Currently, I have a static factory method like this: public static Book Create(BookCode code)
In particular, would it be possible to have code similar to this c++ code
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my
There is probably is simple fix for this but I currently have code similar
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have this code (which is way simplified from the real code): public interface

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.