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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:25:13+00:00 2026-05-23T13:25:13+00:00

This question is related to the one here: configurable dependencies with easy to mock

  • 0

This question is related to the one here: configurable dependencies with easy to mock out default implementations.

My goal is to design small library, following DI pattern. Having code like the following is this right usage of DI?

ValuesConfiguration is just wrapper for Map. It is kind of Value Object, it is created by client in runtime, so there is no possibility to use dependency injection. Previous version (in related question) had this configuration as a constructor argument, but it seemed not to be “real” dependency.

public class Parser {

    private ValuesConfiguration configuration;
    private ValuesProvider valuesProvider;
    private ValuesMapper valuesMapper;

    public Parser() {}

    public Result parse(String parameterName) {
        initDefaults();           
        List<Values> values = valuesProvider.getValues(parameterName);
        ...
        return valuesMapper.transformValues(values, configuration);
    }

    private void initDefaults() {
        if(valuesProvider == null) {
            valuesProvider = DefaultsFactory.getDefaultValuesProvider();
        }
    }

    public void setConfiguration(ValuesConfiguration configuration) {
        this.valuesConfiguration = configuration;
    }

    public void setValuesProvider(ValuesProvider provider) {
        this.valuesProvider = provider;
    }

    ...

}

Isn’t it better to put configuration as a parse() method additional parameter?

  • 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-23T13:25:14+00:00Added an answer on May 23, 2026 at 1:25 pm

    An alternative to Dependency Injection is for a component to discover its own dependencies. It appears to me that you have used the later model.

    If you had used dependency injection you would only pass the values its needed and no more. i.e. all its dependencies are injected not extracted. The component wouldn’t need to know where the values came from.

    A big hint is that your constructor takes no values and your setters are passed generic objects which are not needed once the component has been properly initialised.

    How you could change it to use DI.

    public class Parser {
    
        private final ValuesProvider valuesProvider;
        private final ValuesMapper valuesMapper;
        private final ValuesConfiguration configuratrion;
    
        // all values injected.
        public Parser(ValuesProvider valuesProvider, ValuesMapper valuesMapper, ValuesConfiguration configuratrion) {
            this.valuesProvider = valuesProvider;
            this.valuesMapper = valuesMapper;
            this.configuratrion = configuratrion;
        }
    
        public Result parse(String parameterName) {
            List<Values> values = valuesProvider.getValues(parameterName);
            ...
            return valuesMapper.transformValues(values, configuration);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to one I asked previously, see here . As a
This question is related to this one , though I think I was a
[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
This question is related to a previous post of mine Here . Basically, I
This question is related to the question posted here: Why isn't my custom WCF
This question is related to one I asked the other day which I got
(This question is related to another one , but different enough that I think
This question is related to my last one about jQuery 1.4. They supposedly fixed
This question is related to my last one . I am trying to solve

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.