Why does one use dependency injection

0 votes
I'm still attempting to figure out dependency injections (DI), and I'm failing miserably. It just seems ridiculous. My code is never a jumble; I rarely write virtual functions and interfaces (unless on rare occasions), and all of my configuration is magically serialised into a class using json.net (sometimes using an XML serializer).

I'm not sure what problem it's supposed to solve. It appears to be a method of saying: "hello there. Return an object of this kind that utilises these parameters/data when you encounter this function."
But... why would I ever use something like that? Note that I have never needed to use object as well, but I am aware of its purpose.

What are some real-world examples of when DI would be used in the development of a website or a desktop application? I can think of reasons why someone might wish to use interfaces/virtual functions in a game, but doing so in non-game code is exceedingly rare (to the point where I can't recall a single occasion).
May 31, 2022 in C# by rajiv
• 1,620 points
429 views

1 answer to this question.

0 votes

I believe that many people are perplexed by the distinction between dependency injection and a dependency injection framework (or a container as it is often called).

The concept of dependency injection is extremely straightforward. Instead of this below

public class A {
  private B b;

  public A() {
    this.b = new B(); // A *depends on* B
  }

  public void DoSomeStuff() {
    // Do something with B here
  }
}

public static void Main(string[] args) {
  A a = new A();
  a.DoSomeStuff();
}

One can use this

public class A {
  private B b;

  public A(B b) { // A now takes its dependencies as arguments
    this.b = b; // look ma, no "new"!
  }

  public void DoSomeStuff() {
    // Do something with B here
  }
}

public static void Main(string[] args) {
  B b = new B(); // B is constructed here instead
  A a = new A(b);
  a.DoSomeStuff();
}

That's all there is to it. This provides you with numerous benefits. Two of the most essential are the ability to control functionality from a single location (the Main() function) rather than distributing it throughout your program, and the ability to test each class independently (because you can pass mocks or other faked objects into its function Object() { [native code] } instead of a real value).

The disadvantage is that you now have a single mega-function that knows about all of your program's classes. DI frameworks can assist with this. However, if you're having difficulties grasping why this method is beneficial, I recommend starting with manual dependency injection to gain a better knowledge of what the various frameworks can do for you.

answered Jun 6, 2022 by pranav
• 2,590 points

Related Questions In C#

0 votes
0 answers

Why does one use dependency injection?

I'm making an effort to comprehend dependency ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
507 views
0 votes
1 answer

Switch case: can I use a range instead of a one number

To categorise your switch circumstances, I would ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
402 views
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21, 2022 in C# by krishna
• 2,820 points
641 views
0 votes
1 answer

What is cool about generics, why use them

Okay. For simplification, I am listing it ...READ MORE

answered Jun 23, 2022 in C# by krishna
• 2,820 points
474 views
0 votes
1 answer

Why program working differently on every run when using async?

You can use the async Main method ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
523 views
0 votes
1 answer

What is dependency injection?

According to James Shore, the term “Dependency ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,710 points
686 views
0 votes
1 answer
0 votes
0 answers

How to create dependency injection for ASP.NET MVC 5

It's simple to create Dependency Injection using ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
503 views
0 votes
1 answer

When would you use delegates in C#

Delegates come in handy for a variety ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
529 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP