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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:16:53+00:00 2026-05-25T11:16:53+00:00

We’re using DI and Unity to work with different dependencies (generally, database and repository

  • 0

We’re using DI and Unity to work with different dependencies (generally, database and repository classes, dto to entity mappers, etc)
Right now we’re trying to create smaller functions that perform tasks that try to be independend from each other, in order to increase testability and also to avoid methods that have lots of different responsibilities, to avoid coupling

One question that I have is, how should DI be used when we have methods that rely on other inner methods, when the nesting is not trivial. For example, consider the following example (just a concept, not real working code):

public ProcessedOrder ProcessOrders(Order inputOrder)
{
    foreach (var line in inputOrder.OrderLines)
    {
         var someData = LineProcessor(line);
    }
}

public SomeData LineProcessor(OrderLine line)
{
      /* do some stuff*/
      var OtherData = ThingProcessor(null,line.SomeStuff);
      var ret = new SomeData();
      // assign values, more stuff
      return ret;
}

public OtherData ThingProcessor(IDep1 someDependency, SomeStuff stuff)
{
      someDependency = someDependency ?? ServiceLocator.Resolve<IDep1>();
      var ret = someDependency.DoThings(stuff);
      return ret;
}

Ok, so far the example shows that we have 3 functions, that could theoretically be called on their own. there’s some injected dependency in the ThingProcessor, and if it’s null then it tries to resolve it.
However, this is a somehow simple example, but I see something I don’t like so much. For instance, I’m calling ThingProcessor with a null value in the first param. So I can say, ok, I modify the signature of LineProcessor to have it injected, so that he pass it in to the other function that needs it. However, he really doesn’t need it, it’s not its dependency but the function that he’s calling.
So here I don’t know what approach is the more correct one, if the one that i’m showing, or if I should pass the correlated dependencies across layers. If I do this last thing, then the outermost function will be a mess, because it’ll have a long list of dependencies that it will feed to everyone that’s below it.
However, the “null approach” I don’t like very much, so I’m pretty sure that something’s wrong somewhere, and there’s probably a better way to design this.
What’s the best approach??? Remember, all functions must be used independently (called on their own), so for example I may call just ThingProcessor at some point, or at another one only LineProcessor.

UPDATE :

public CommonPurposeFunctions(IDep1 dep1, IDep2 dep2 ....)
{  
      this.Dep1 = dep1;
      this.Dep2 = dep2;
      [...]

}

public ProcessedOrder ProcessOrders(Order inputOrder)
{
    foreach (var line in inputOrder.OrderLines)
    {
         var someData = LineProcessor(line);
    }
}

public SomeData LineProcessor(OrderLine line)
{
      /* do some stuff*/
      var OtherData = ThingProcessor(line.SomeStuff);
      var ret = new SomeData();
      var morethings = this.Dep2.DoMoreThings();
      // assign values, more stuff
      return ret;
}

public OtherData ThingProcessor(SomeStuff stuff)
{
      var ret = this.Dep1.DoThings(stuff);
      return ret;
}
  • 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-25T11:16:54+00:00Added an answer on May 25, 2026 at 11:16 am

    The approach we use is constructor injection, then we store the dependency in a private member field. The container wires up the dependencies; so the number of classes and constructor parameters doesn’t really matter.

    This works for services. If the dependencies across calls have meaningful state, you will have to pass them in to each call. But, in that case, I’d question if the methods really need to be public methods in their own classes.

    You want to end up with a design that eliminates the service locator and truly injects the dependencies.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

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.