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

The Archive Base Latest Questions

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

Currently I’m trying to understand dependency injection better and I’m using asp.net MVC to

  • 0

Currently I’m trying to understand dependency injection better and I’m using asp.net MVC to work with it. You might see some other related questions from me 😉

Alright, I’ll start with an example controller (of an example Contacts Manager asp.net MVC application)

public class ContactsController{

  ContactsManagerDb _db;

  public ContactsController(){

    _db = ContactsManagerDb();

  } 

  //...Actions here
}

Allright, awesome that’s working. My actions can all use the database for CRUD actions. Now I’ve decided I wanted to add unit testing, and I’ve added another contructor to mock a database

public class ContactsController{

  IContactsManagerDb _db;

  public ContactsController(){

    _db = ContactsManagerDb();

  }

  public ContactsController(IContactsManagerDb db){
    _db = db;
  }

  //...Actions here
}

Awesome, that’s working to, in my unit tests I can create my own implementation of the IContactsManagerDb and unit test my controller.

Now, people usually make the following decision (and here is my actual question), get rid of the empty controller, and use dependency injection to define what implementation to use.

So using StructureMap I’ve added the following injection rule:

x.For<IContactsManagerDb>().Use<ContactsManagerDb>();

And ofcourse in my Testing Project I’m using a different IContactsManagerDb implementation.

x.For<IContactsManagerDb>().Use<MyTestingContactsManagerDb>();

But my question is, **What problem have I solved or what have I simplified by using dependency injection in this specific case”

I fail to see any practical use of it now, I understand the HOW but not the WHY? What’s the use of this? Can anyone add to this project perhaps, make an example how this is more practical and useful?

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

    The first example is not unit testable, so it is not good as it is creating a strong coupling between the different layers of your application and makes them less reusable. The second example is called poor man dependency injection. It’s also discussed here.

    What is wrong with poor man dependency injection is that the code is not autodocumenting. It doesn’t state its intent to the consumer. A consumer sees this code and he could easily call the default constructor without passing any argument, whereas if there was no default constructor it would have immediately been clear that this class absolutely requires some contract to be passed to its constructor in order to function normally. And it is really not to the class to decide which specific implementation to choose. It is up to the consumer of this class.

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

Sidebar

Related Questions

Currently I have some legacy ASP.NET 2.0 code that uses the ASP Xml web
Currently IIS sends an expires http header of yesterday minus 1 hour on ASP.NET
Currently I use [Authorize(Roles = .....)] to secure my controller actions on my ASP.NET
Currently I'm trying to parse some html and return an array with the values
Currently we use log4net of version 1.2.10.0 and we should start using some 3rd
Currently I am testing some RTL, I am using ncverilog, and it is very
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, I am developing a product that does fairly intensive calculations using MS SQL
Currently I using the following code to get my JSON output from MySQL. <?php

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.