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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:18:06+00:00 2026-05-26T01:18:06+00:00

I understand dependency injection, but have not had that ah moment, where it clicks

  • 0

I understand dependency injection, but have not had that “ah” moment, where it clicks and I really see the light.

Why should I use DI? Also, when mocking objects like those which use the file system, what would the mock object be capable of? Does it just do dummy calls (so does not really use the file system)?

  • 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-26T01:18:06+00:00Added an answer on May 26, 2026 at 1:18 am

    Let me go a couple steps farther from hvgotcodes answer:

    class Service {
       Collaborator c = new Collaborator()
    }
    

    is your original class with the hard-coded dependency.

    class Service {
        Collaborator c;
    
        Service(Collaborator c) {
           this.c = c;
        }
    }
    

    is your newfangled class with the injected dependency.

    So far, so good. Now, let’s take Collaborator and extract an interface from it; call it ICollaborator. Now your newerfangled class looks like such:

    class Service {
        ICollaborator c;
    
        Service(ICollaborator c) {
           this.c = c;
        }
    }
    

    What does this buy you? Well, you can, in your code, create this class to behave like the first example as such:

    // w00t!  My code compiles and works again!  Ship it!
    Service myService = new Service(new Collaborator());
    

    Pretty cut and dry easy. The beauty comes from when you want to use a different type of Collaborator — perhaps even a mock or fake. So long as it implements the ICollaborator interface, you’re golden:

    // I'm using Fake It Easy for this example.
    Service myService = new Service(A.Fake<ICollaborator>());
    

    Voila! You now have a unit-testable Service instance that doesn’t drag the concrete Collaborator along for the ride (which would be breaking true “unit” testing).

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

Sidebar

Related Questions

I have tried to understand dependency injection and not quite gotten it, except I
I have tried to understand AOP, Dependency Injection and Inversion of Control SPRING related
I have several articles about Dependency Injection, and I can see the benefits, especially
I've been reading about dependency injection and have a simple question. I can understand
I have worked with code which had NUnit test written. But, I have never
I've been reading up on dependency injection and I understand the appeal of specifying
So on a high level I understand the concept of dependency injection. Our company
I have been practicing constructor dependency injection throughout my PHP application. I didn't want
I've been reading up on dependency injection and I think I pretty much understand
I've got dependency injection working with Unity and it runs nicely but I want

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.