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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:10:58+00:00 2026-05-16T07:10:58+00:00

I’m having trouble designing classes to best make use of DI / IoC principles,

  • 0

I’m having trouble designing classes to best make use of DI / IoC principles, particularly when a class shares a dependency with one of its dependencies (ie. X has dependencies Y and Z, and Y has dependency Z).

An example will probably be useful:

Lets say I have a class that will encapsulate some connection information for my app. Call it ‘ConnectionInfo’

class ConnectionInfo
{
    // ...
}

I’ve designed a worker class that uses the connection info, so I’ll be injecting my dependencies through the constructor.

class Worker 
{
    public Worker(ConnectionInfo c) 
    {
        // ...
    }
}

Now, lets say I want to build another class. It needs access to the connection info, but I also want to have access to the functionality provided by the Worker class.

class AnotherClass
{
    public AnotherClass(ConnectionInfo c, Worker w)
    {
       // ...
    }
}

This is all well and good. However, there is an implied logical relationship between the ConnectionInfo object injected into the Worker, and the ConnectionInfo object injected into the AnotherClass. The design only makes sense when they are one and the same. This isn’t enforced, though. There is nothing stopping one ConnectionInfo being injected into AnotherClass, and a completely unrelated ConnectionInfo being injected into Worker.

Am I dealing with IoC / DI in the wrong way?
Should the classes be designed differently, or should the issues be handled in another way (such as enforcing via parameter validation, or maybe just leaving it up to the IoC container)?

  • 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-16T07:10:58+00:00Added an answer on May 16, 2026 at 7:10 am

    i don’t think that there is a problem with the Class design

    when your client code needs an object of AnotherClass than it will create it’s own dependencies of ConnectonInfo and Worker.

    There are actually 2 scenarios.

    1- Is ur ConnectionInfo and Worker are Singletons : in that case, if you create an object of AnotherClass than same objects of ConnectionInfo and Worker will be shared by the AnotherClass.

    2- If point 1 is not true : Than every time you create your AnotehrClass, Container will inject dependencies as a fresh objects.

    But for ConnectionInfo , i might say that it may be a Sigleton object becuase it’s the common information which needs by your application so i would say that make ConectionInfo as Singleton and rest is fine , i don’t think you are violating any IoC rules.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.