0

I'm putting together a unit testing training class for C# developers. I can debug through the code and see what's happening in some simple dependency injection examples. But I can't find the words / phrases to accurately describe what is happening when we create a class object and pass it a new Service.

Two questions.

How is this

var test = new Service();
Client client = new Client(test);

different that this?

Client client = new Client(new Service());

And what is the term used for the Service in this example?

new Client(new Service());
7
  • Where is the dependency injection here? Without defining Service and Client this is just a slow Tuesday in C#. Commented Jun 13, 2018 at 5:04
  • @PepitoSh When you inject a dependency (here Service via constructor) you have Dependency Injection. Commented Jun 13, 2018 at 5:11
  • 1
    There is no difference, and it’s called a dependency. Commented Jun 13, 2018 at 5:16
  • Passing an object in a constructor does not qualify as dependency injection. The passed object must meet certain criteria, implement interfaces, etc. This is why I find this question vague. Commented Jun 13, 2018 at 5:21
  • @PepitoSh If Client depends on Service and you can control the Service from outside (pass it via constructor or setter) we are talking about DI or IoC. Period. Wikipedia A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state.[1] Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. Commented Jun 13, 2018 at 5:30

1 Answer 1

2
var test = new Service();
Client client = new Client(test);

In this example, the Service dependency is stored in a variable and is injected into the consumer Client, using constructor injection.

Client client = new Client(new Service());

In this example, the Service dependency is injected directly into Client's constructor.

And what is the term used for the Service in this example?

In this case, Service is the dependency and Client is the consumer. They are both components. In case Client depends on IService, IService is the service or abstraction, while Service is its implementation, or more generally: the component.

A component is an application class that contains application logic. Components are the vocal point of DI, since you build up object graphs of components. The opposite or components are data centric objects, such as entities, DTOs, messages, view models, etc. They don't contain any behavior and are typically passed through method calls on initialized object graphs.

For a complete terminology, see Dependency Injection Principles, Practices, and Patterns.

Sign up to request clarification or add additional context in comments.

1 Comment

The first couple of paragraphs might be a source code distinction, as the final IL will be identical unless you're using the test variable for something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.