Dependency Injection Flashcards

1
Q

What is DI

A
a programming technique that makes a class independent of its dependencies
DI achieves inversion of control (IoC)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does DI work

A

by decoupling the usage of an object from its creation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What problem does DI address

A

SOLID design principles to improve reusability of code and reduce the frequency with which you need to change a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does DI accomplish its goal(s)

A

by decoupling the creation of the usage of an object. That enables you to replace dependencies without changing the class that uses them.

Dependencies are configured at run-time.

It also reduces the risk that you have to change a class just because one of its dependencies changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an alternative to DI

A

The service locator pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Role 1 in DI

A

A service you want to use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Role 2 in DI

A

A client that uses the service

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Role 3 in DI

A

An interface that is used by the client and implements the service

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Role 4 in DI

A

An injector which creates a service instance and injects it into the client

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between dependency inversion and DI

A

Dependency inversion includes roles 1-3 in DI but not the fourth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does constructor injection allow

A

Constructor injection enables you to replace the compile time dependency to a specific implementation class with a runtime dependency to any implementation class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Setter Injection

A

Allows you to remove dependency through the properties of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Interface-based Injection

A

Injection is made directly into a class through a method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Service Lifetimes

A

Transient, Scoped, Singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Service Lifetime - Transient

A

Transient lifetime services are created each time they’re requested from the service container. This lifetime works best for lightweight, stateless services. Register transient services with AddTransient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Service Lifetime - Scoped

A

Created once per scope ie we request or any unit of work

17
Q

Service Lifetime - Singleton

A

Created only for first request. If a particular instance is specified at registration time, this instance will be provided to all consumers of the registration type