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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:52:15+00:00 2026-05-24T20:52:15+00:00

This question is about testable software design based on mostly value objects and services.

  • 0

This question is about testable software design based on mostly value objects and services.

Here’s an example of an API of a simple service that can save data to files.

saveToFile(data, fileName)
saveToUniqueFile(data, fileNameGenerator)

fileNameGenerator is a service that generates random file names. It is used to find a unique file name to save data to. In this example fileNameGenerator is injected as a method parameter.

One of the alternatives is constructor injection which would simplify the API:

saveToFile(data, fileName)
saveToUniqueFile(data)

Saving to a unique file certainly won’t be used every time, so it would seem that a mandatory constructor parameter should not be required. On the other hand, services usually communicate via data and here we have a service and it does clutter the API a bit.

Are there any potential problems/inconveniences of passing services around as method parameters? Should constructor injection still be preferred in such situations?

  • 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-24T20:52:15+00:00Added an answer on May 24, 2026 at 8:52 pm

    Passing services around as parameters is quite problematic, because you never know when you need them. Methods and their parameters constitute your API, whereas the constructor doesn’t. Using the constructor to inject services gives you a much larger degree of freedom because it allows you to decouple dependencies from the API expressed by the methods.

    Otherwise you’d have to pass the method argument around to all methods in your API on the off chance that one or two of them might need it.

    Even when a service is only used once in a while, it’s rarely an issue to use inject them via constructors.

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

Sidebar

Related Questions

This question about Timers for windows services got me thinking: Say I have (and
This is a question about style and design rather than syntax. I have domain
Read this question about how to compare custom objects in C++. I did not
I have a simple basic question about C# and inheritance. In the below example,
This is a follow up based on this question about query optimization . In
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers
I have seen this question about deploying to WebSphere using the WAS ant tasks.

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.